<分区>
我想允许用户根据他们的字符串输入参数导入模块。
即
$python myprogram.py --import_option xyz.py
在 python 中,我在解析一个参数后想要这样的东西。
arg = 'xyz.py'
import arg #assuming that the module name xyz.py exists.
<分区>
我想允许用户根据他们的字符串输入参数导入模块。
即
$python myprogram.py --import_option xyz.py
在 python 中,我在解析一个参数后想要这样的东西。
arg = 'xyz.py'
import arg #assuming that the module name xyz.py exists.
最佳答案
>>> a='math'
>>> import importlib
>>> i=importlib.import_module(a)
>>> i.sqrt(4)
2.0
关于python - 在 python 中,如何通过用户输入模块名称的字符串来导入模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26076986/