4

I want to allow the user to import a module based on their string input argument.

i.e

$python myprogram.py --import_option xyz.py

In python I want something like this after parsing an argument.

arg = 'xyz.py'

import arg #assuming that the module name xyz.py exists.
Dhruv Patel
  • 426
  • 3
  • 9
  • 19

1 Answers1

11
>>> a='math'
>>> import importlib
>>> i=importlib.import_module(a)
>>> i.sqrt(4)
2.0
g4ur4v
  • 3,210
  • 5
  • 32
  • 57