This question already has answers here:
Imports in __init__.py and `import as` statement

(4个答案)


1年前关闭。




我有一个very simple namespace package(内容包括在下面,以及目录布局)。如果我尝试导入namespace_repro.module,则会出现以下错误:AttributeError: module 'namespace_repro' has no attribute 'module'。据我了解,我的软件包布局有效,导入应该可以正常进行。有趣的是,该错误仅在Python 3.6.8中存在,并且导入在Python 3.7中成功。

如何重现问题?

我有一个名为import-error-repro的目录,其中有一个setup.py(请参见下文),然后是一个嵌套的目录路径src/namespace_repro/module,其中包含三个文件__init__.pyx.pyy.py。它们的内容:
setup.py
from setuptools import find_namespace_packages, setup

setup(
    name='namespace-repro',
    version='0.1.0',
    python_requires='>=3.6',
    packages=find_namespace_packages('src'),
    package_dir={'': 'src'},
    zip_safe=False,
)
src/namespace_repro/module/__init__.py:
from namespace_repro.module.x import x
src/namespace_repro/module/x.py:
import namespace_repro.module.y as y

x = y.y

最后src/namespace_repro/module/y.py:
y = True

我通过conda create -n namespace6 python=3.6 ipython创建了一个全新的Python 3.6 conda环境,然后将其激活并以pip install -e ./import-error-repro的形式安装了该软件包(请注意-e无关紧要,没有它,该问题是可以重现的)。在那之后,我尝试了import namespace_repro.module中的ipython(尽管在官方的python解释器中也是如此)。结果是
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-bcae5a697dad> in <module>
----> 1 import namespace_repro.module

~/namespace-repro/src/namespace_repro/module/__init__.py in <module>
----> 1 from namespace_repro.module.x import x

~/namespace-repro/src/namespace_repro/module/x.py in <module>
----> 1 import namespace_repro.module.y as y
      2
      3 x = y.y

AttributeError: module 'namespace_repro' has no attribute 'module'
---------------------------------------------------------------------------

奇怪的是,导入系统两次找到namespace_repro.module,但第三次失败!

其他一些有趣的行为:
In [1]: import namespace_repro.module.y as y  # This doesn't work.
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-4035347ea59b> in <module>
----> 1 import namespace_repro.module.y as y

AttributeError: module 'namespace_repro' has no attribute 'module'

In [2]: import namespace_repro.module.y  # But this one does! Why?

In [3]: dir(namespace_repro.module.y) # The error returns when we actually want to use the module.
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-d89bcfd9e509> in <module>
----> 1 dir(namespace_repro.module.y)

AttributeError: module 'namespace_repro' has no attribute 'module'

In [4]: from namespace_repro.module.y import y  # This works fine!

In [5]: y
Out[5]: True

目录布局
. import-error-repro
+-- setup.py
+-- src
|   +-- namespace_repro
|   |   +-- module
|   |   |   +-- __init__.py
|   |   |   +-- x.py
|   |   |   +-- y.py

最佳答案

这是CPython错误30024,毫不奇怪的是,它已在3.7中修复。请注意,从3.5开始,具有相对(圆形)导入的更惯用的形式开始起作用。

关于python - 如何解决Python 3.6中的导入错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56394350/

10-11 22:34
查看更多