Python:使用AttributeError使用动态加载的模块失败

发布于 2025-02-02 16:46:19 字数 1348 浏览 5 评论 0原文

环境

  • python:3.9
  • pycharm下的virtualenv

在我写的当前源代码中,我尝试:

  1. 动态检查是否安装了所需的依赖项
  2. ,如果没有,会出现消息,并且程序停止,否则请继续到3
  3. 源代码动态导入已安装的模块,
  4. 当需要时,代码像往常一样使用该模块,

则代码是用 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:

  1. Dynamically check if the required dependencies are installed
  2. If not, a message appears and the program stops, otherwise, go to 3
  3. The source code dynamically imports the installed module
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素食主义者 2025-02-09 16:46:19

一切都在源代码中起作用。

问题在于,我在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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文