Python:尝试从导入的包中导入模块时出现 'ModuleNotFoundError'

标签 python python-3.x python-import

我在 macOS Mojave 版本 10.14.1 上使用 Python 3.7.1

这是我的目录结构:

man/                          
  Mans/                  
          man1.py
  MansTest/
          SoftLib/
                  Soft/
                      SoftWork/
                              manModules.py
          Unittests/
                    man1test.py
man1.py包含以下导入语句 我不想改变 :
from Soft.SoftWork.manModules import *
man1test.py包含以下导入语句:
from ...MansTest.SoftLib import Soft
from ...Mans import man1

我需要在 man1test.py 中进行第二次导入因为man1test.py需要访问 man1.py 中的函数.

我在第一次导入(软)背后的基本原理是为了方便 man1.py 中的上述导入语句。 .

然而,与我的预期相反,man1.py 中的导入语句导致:
ModuleNotFoundError: No module named 'Soft'

当我运行时
python3 -m man.MansTest.Unittests.man1test

从 man/上面的目录中。

有什么方法可以在不更改 man1.py 中的导入语句的情况下解决此错误? 不向 sys.path 添加任何内容?

编辑:python3 -m man.ManTest.Unittests.man1test从问题的原始版本更改为 python3 -m man.MansTest.Unittests.man1test

最佳答案

第一 , 如果您希望能够从 man1test.py 和 manModules.py 从 man1.py 访问 man1.py,您需要将文件正确设置为 packages and modules .

Packages are a way of structuring Python’s module namespace by using “dotted module names”. For example, the module name A.B designates a submodule named B in a package named A.

...

When importing the package, Python searches through the directories on sys.path looking for the package subdirectory.

The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path.


您需要将其设置为以下内容:
man
|- __init__.py
|- Mans
   |- __init__.py
   |- man1.py
|- MansTest
   |- __init.__.py
   |- SoftLib
      |- Soft
         |- __init__.py
         |- SoftWork
            |- __init__.py
            |- manModules.py
      |- Unittests
         |- __init__.py
         |- man1test.py
第二 ,对于由 ModuleNotFoundError: No module named 'Soft' 引起的“from ...Mans import man1”错误在 man1test.py 中,记录在案的解决方案是将 man1.py 添加到 sys.path因为 Mans 在 MansTest 包之外。见 The Module Search Path来自 Python 文档。但是如果不想修改sys.path也可以直接修改PYTHONPATH :

sys.path is initialized from these locations:

  • The directory containing the input script (or the current directory when no file is specified).
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • The installation-dependent default.

第三 , 对于 from ...MansTest.SoftLib import Soft您所说的“是为了促进 man1.py 中的上述导入语句”,这就是导入的工作方式。如果要在 man1.py 中导入 Soft.SoftLib,则必须设置 man1.py 以找到 Soft.SoftLib 并直接将其导入。
话虽如此,这就是我如何让它工作的。
人1.py:
from Soft.SoftWork.manModules import *
# no change to import statement but need to add Soft to PYTHONPATH

def foo():
    print("called foo in man1.py")
    print("foo call module1 from manModules: " + module1())
man1test.py
# no need for "from ...MansTest.SoftLib import Soft" to facilitate importing..
from ...Mans import man1

man1.foo()
人模块.py
def module1():
    return "module1 in manModules"
终端输出:
$ python3 -m man.MansTest.Unittests.man1test
Traceback (most recent call last):
  ...
    from ...Mans import man1
  File "/temp/man/Mans/man1.py", line 2, in <module>
    from Soft.SoftWork.manModules import *
ModuleNotFoundError: No module named 'Soft'

$ PYTHONPATH=$PYTHONPATH:/temp/man/MansTest/SoftLib
$ export PYTHONPATH
$ echo $PYTHONPATH
:/temp/man/MansTest/SoftLib
$ python3 -m man.MansTest.Unittests.man1test
called foo in man1.py
foo called module1 from manModules: module1 in manModules 

作为一个建议,也许重新考虑那些 SoftLib 文件的目的。它是 man1.py 和 man1test.py 之间的某种“桥梁”吗?您现在的文件设置方式,我认为它不会像您期望的那样工作。此外,被测代码 (man1.py) 从测试文件夹 (MansTest) 下导入内容有点令人困惑。

关于Python:尝试从导入的包中导入模块时出现 'ModuleNotFoundError',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54598292/

相关文章:

javascript - Bokeh 中的自定义 javascript 回调可从数据框中选择列并更新绘图

python - 从子模块上的模块调用函数

python - 模块 'shapely' 没有属性 'geometry' 错误

python - 算法 : selecting points from a list

python - Django 1.6 将下拉选择值传递给另一个模板

python - 如何使用 python 按位置计算字符串中相同字符的数量?

python - 有没有办法抑制 TensorFlow 打印的消息?

python - 无法将图像保存到 django 模型

python - 如何升级pip3?

python - 相对导入 - ModuleNotFoundError : No module named x