In python, How to import a module by user input string of a module name
本问题已经有最佳答案,请猛点这里访问。
我希望允许用户基于其字符串输入参数导入模块。
即
| 1 | $python myprogram.py --import_option xyz.py | 
在python中,在解析参数之后,我需要类似的东西。
| 1 2 3 | arg = 'xyz.py' import arg #assuming that the module name xyz.py exists. | 
| 1 2 3 4 5 | >>> a='math' >>> import importlib >>> i=importlib.import_module(a) >>> i.sqrt(4) 2.0 |