我尝试了以下内容:
>> modname = 'sys'
>> import modname
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: No module named modname
为什么导入无法将'sys'识别为要导入的模块的名称?
答案 0 :(得分:4)
importlib
模块。
>>> import importlib
>>> mod = importlib.import_module('sys')
>>> mod
<module 'sys' (built-in)>
答案 1 :(得分:2)
>>> modname = 'sys'
>>> sys = __import__(modname)