无法让 __import__() 在 python 中动态导入模块 - 我知道这是因为它不会出现在 sys.modules 中
我写了一个小脚本。它的设计目的是在 python 目录中搜索所有可用模块(无论是否安装),然后检查当前加载的模块,然后提供动态加载您选择的模块的选项。后者使用 __import__() 因为我向它传递了一个字符串 - (这是我遇到问题的地方 - 但我很快就会回到它)......然后它给出了选项“浏览”模块的所有类、函数等(使用 dir([模块名称]) ...)。
问题:
当模块动态加载时 - 它嵌入在 try/ except
语句中 - 如果成功,则报告“模块已加载”,如果失败它报告...呃...“无法加载...”
如果您键入模块的名称,例如名为“uu”的模块,它会显示“已加载”。所以我知道它正在加载 - 但是,当我返回并调用检查所有已加载模块的函数时 - 它是空白的(使用 sys.modules )
我认为 python 正在加载模块进入一个不是 sys.modules 的临时位置,因为当我退出脚本并检查 sys.modules 时它不在那里。
I wrote a small script. It's designed to search the python directory for all available modules (whether they are installed or not), then it is supposed to check what modules are currently loaded, then it offers an option to dynamically load a module of your choice. The latter using __import__()
because I am passing a string to it - (this is where I am having a problem - but I'll get back to it shortly)...then it gives the option to "browse" the module for all its classes, functions, etc. (using dir([module name])
...).
The problem:
When the module is loaded dynamically - it is embedded in a try/except
statement - if it succeeds it reports that the "module is loaded" and if it fails it reports...duh..."Failed to load..."
If you type the name of a module, for example a module named "uu", it says "loaded". So I know it is loading - however, when I go back and call the function that checks all of the LOADED modules - it is blank (using sys.modules
)
I am thinking that python is loading the module into a temporary place which is not sys.modules
because when I exit out of the script and check sys.modules
it is not there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Nascent_Notes,不错的脚本!
我尝试加载
uu
(命令 3)并打印已加载模块的列表(命令 2),它们似乎都工作正常。但是,如果我尝试“浏览模块”(命令 4),则会收到以下错误:
尝试运行,
您应该收到
NameError: name 'uu' is not Defined
。看来虽然
__import__
成功导入了uu
模块,它不会将
uu
添加到全局命名空间 - 模块uu
不能通过变量名
uu
访问。但是,可以通过 sys.modules 访问它:因此,更改
为
使用
raw_input
不仅比input
安全得多(用户不会能够执行意外/恶意命令),而且raw_input
也可以在这里执行您想要的操作。需要注意的是,您还可以更改
为更Pythonic的
编辑:
sys.modules是一个dict(字典的缩写)。字典就像电话簿——你给它一个名称(更好地称为“键”),它返回一个电话号码(或更一般地说,一个“值”)。
对于 sys.modules,键是模块名称(字符串)。这些值是模块对象本身。
您可以使用括号表示法访问字典中的值。所以
uu
只是一个字符串,但是sys.modules['uu']
是模块uu
。您可以在这里阅读有关字典的完整故事:http://docs.python.org/教程/datastructs.html#dictionaries
Nascent_Notes, nice script!
I tried loading
uu
(command 3) and printing the list of loaded modules (command 2) and they both seem to work fine.However, if I try to "browse the module" (command 4), I get the following error:
Try running
You should get
NameError: name 'uu' is not defined
.So it appears that although
__import__
successfully imports theuu
module,it does not add
uu
to the global namespace -- the moduleuu
can not beaccessed by the variable name
uu
. It can be accessed throughsys.modules
however:Therefore, change
to
Not only is using
raw_input
much safer thaninput
(the user will not be able to execute unexpected/malicious commands), but alsoraw_input
does what you want here.On a minor note, you could also change
to the more pythonic
Edit:
sys.modules is a dict (short for dictionary). Dicts are like telephone books -- you give it a name (better known as a "key") and it returns a phone number (or more generally, a "value").
In the case of sys.modules, the keys are module names (strings). The values are the module objects themselves.
You access the values in the dict using bracket notation. So
uu
is just a string, butsys.modules['uu']
is the moduleuu
.You can read the full story on dicts here: http://docs.python.org/tutorial/datastructures.html#dictionaries