python - 导入错误:没有命名的模块

标签 python importerror

我有一个项目,其文件夹结构如下:

project:
    folder1:
        __init__.py
        file11.py
    folder2:
        __init__.py
        file21.py
    folder3:
        __init__.py
        file31.py
    __init__.py
    file1.py
    file2.py
    ..

从项目的根目录,我可以导入同一路径上的文件

示例,

file1.py 中,from file2 import 123from folder3.file31 import 456 有效

file21.py中,import file1 有效

执行此操作时会触发我的问题:

示例,

file21.py中,from folder3.file31 import xyz

这会抛出一个错误,例如 ImportError:没有名为folder3 的模块

我所有的__init__.py文件都是空的,让python相信它们是包。我不明白为什么会发生这种情况。我想知道这个问题的解决方案以及关于它如何工作的一些小见解。

最佳答案

模块file31.py位于包project.folder3内,而不仅仅是project内,也不与模块file21在同一包内.py。尝试在 file21.py 中使用:

from folder3.file31 import xyz

而不是

from file31 import xyz

关于python - 导入错误:没有命名的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39243647/

相关文章:

python - 为 Django 的日期输入设置添加一个值

python - 获取每列二维数组中最后一个负值的索引

python - python 保护文件的最佳方法?

python - '导入错误 : No module named pytz' when trying to import pylab?

python - cPickle 的导入错误

python - NumPy 库导入错误 : DLL load failed: The specified procedure could not be found

python - Netaddr 返回 NameError 而不是 ImportError

python - 具有 Minimax 和 Alpha-beta 修剪的 3D Tic Tac Toe 选择次优移动

python - 如何在数据库之前格式化变量以避免错误

python - ImportError:/home/test/test_1.so: 来自 cython 构建的错误 ELF 类