[英]How do I import modules from an array in python?
Im trying to import a set of modules that are defined by an array. 我正在尝试导入由数组定义的一组模块。 The reason I would like to store them in an array is because I would like to change this list dynamically. 我想将它们存储在数组中的原因是因为我想动态更改此列表。
moduleNames = ['sys', 'os']
import moduleNames
However this doesnt work. 但是,这不起作用。
I have searched around and found this: 我搜索了一下,发现了这一点:
moduleNames = ['sys', 'os']
modules = map(__inport__, moduleNames)
This imports the modules however I would like to be able to access it by doing sys.version
for example. 这将导入模块,但是我希望能够通过执行sys.version
来访问它。 Rather than by using modules[0].version
而不是通过使用modules[0].version
Is this currently possible I can't seem to find anywhere that shows you how to do it. 目前是否有可能,我似乎找不到任何可以向您展示如何操作的地方。
How about 怎么样
for m in modules:
globals()[m] = __import__(m)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.