Python:使用AttributeError使用动态加载的模块失败
环境:
- python:3.9
- pycharm下的virtualenv
在我写的当前源代码中,我尝试:
- 动态检查是否安装了所需的依赖项
- ,如果没有,会出现消息,并且程序停止,否则请继续到3
- 源代码动态导入已安装的模块,
- 当需要时,代码像往常一样使用该模块,
则代码是用 tkfilebrowser 模块在(4)上失败的。这是我的导入代码:
already_imported = {}
def do_import(dependency):
"""
Imports the dependency module
:param dependency: the name of the dependency module to import
:raise: ModuleNotFoundError if not found
:return: the imported module
"""
global already_imported
if dependency in already_imported:
result_module = already_imported[dependency]
else:
result_module = importlib.import_module(dependency)
already_imported[dependency] = result_module
return result_module
在通话后,这就是上下文:
>>> tkfilebrowser = do_import('tkfilebrowser')
>>> dir(tkfilebrowser)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
>>> type(tkfilebrowser)
<class 'module'>
>>> print(tkfilebrowser)
<module 'tkfilebrowser' (namespace)>
>>> tkfilebrowser.askopendirname()
AttributeError: module 'tkfilebrowser' has no attribute 'askopendirname'
我无法猜测这里有什么问题以及正确使用模块的方法。
Environment:
- Python: 3.9
- Virtualenv under PyCharm
In the current source code I'm writing I try to:
- Dynamically check if the required dependencies are installed
- If not, a message appears and the program stops, otherwise, go to 3
- The source code dynamically imports the installed module
- The code uses the module as usual when it is required
The thing is that the code is failing at (4) with the tkfilebrowser module. This is my code for importing:
already_imported = {}
def do_import(dependency):
"""
Imports the dependency module
:param dependency: the name of the dependency module to import
:raise: ModuleNotFoundError if not found
:return: the imported module
"""
global already_imported
if dependency in already_imported:
result_module = already_imported[dependency]
else:
result_module = importlib.import_module(dependency)
already_imported[dependency] = result_module
return result_module
Just after the call, this is the context:
>>> tkfilebrowser = do_import('tkfilebrowser')
>>> dir(tkfilebrowser)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
>>> type(tkfilebrowser)
<class 'module'>
>>> print(tkfilebrowser)
<module 'tkfilebrowser' (namespace)>
>>> tkfilebrowser.askopendirname()
AttributeError: module 'tkfilebrowser' has no attribute 'askopendirname'
I couldn't guess what's wrong here and what I should be doing for using the module correctly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一切都在源代码中起作用。
问题在于,我在Pycharm的运行配置中使用了不正确的Python二进制文件,因此,一旦意识到,我就改变了它,一切都很好。
因此,如果有人出于类似的错误来到这里,只需确保您的Virtualenv或您配置的任何Python环境实际上是在使用而不是另一个环境。
在pycharm中:
编辑配置=&gt; Python解释器
Everything was working in the source code.
The problem was that I was using an incorrect Python binary in the Run configuration of PyCharm so, as soon as I realized, I changed it and all come along well.
So if anyone comes here with a similar error, just make really sure that your virtualenv or whatever is the Python environment you have configured is really being used and not another one.
In PyCharm:
Edit configurations => Python interpreter