gpt4 book ai didi

python - "import lib.subpackage.blah as foo"问题 - "AttributeError: module ' lib ' has no attribute ' 子包 '"

转载 作者:行者123 更新时间:2023-12-01 02:16:14 24 4
gpt4 key购买 nike

我的文件系统结构:

  • main.py .
  • lib/
    • __init__.py .
    • subpackage/
      • __init__.py .
      • app.py
      • sched.py

ma​​in.py:

import lib.subpackage

lib/__init__.py 为空。

lib/subpackage/__init__.py:

import lib.subpackage.sched
import lib.subpackage.app

lib/subpackage/app.py:

import lib.subpackage.sched as foo

lib/subpackage/sched.py 为空。

<小时/>

我收到此错误:

Traceback (most recent call last):
File "c:\Users\logix\Desktop\code_\from_linux\dbg\main.py", line 1, in <module>
import lib.subpackage
File "c:\Users\logix\Desktop\code_\from_linux\dbg\lib\subpackage\__init__.py", line 2, in <module>
import lib.subpackage.app
File "c:\Users\logix\Desktop\code_\from_linux\dbg\lib\subpackage\app.py", line 1, in <module>
import lib.subpackage.sched as foo
AttributeError: module 'lib' has no attribute 'subpackage'

我正在从命令行使用 Python 3.6.4。

有什么想法吗?

最佳答案

这是使用别名时导入机制中的一个已知错误;报告为 issue #23203 :

Aliasing import of sub-{module,package} from the package raises AttributeError on import.

We have three nested packages: foo -> bar -> baz. The bar package imports foo.bar.baz. We try to import foo.bar. This works well unless we try to alias the foo.bar.baz import in foo.bar with the import ... as ... syntax.

此错误修复是 Python 3.7 的一部分(目前仅在 alpha 版本中)。

解决方法是使用相对导入:

from . import sched as foo

关于python - "import lib.subpackage.blah as foo"问题 - "AttributeError: module ' lib ' has no attribute ' 子包 '",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48356906/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com