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.
0

1 Answer 1

11
>>> a='math'
>>> import importlib
>>> i=importlib.import_module(a)
>>> i.sqrt(4)
2.0
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.