gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:12:51 27 4
gpt4 key购买 nike

我在 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 asubmodule named B in a package named A.

...

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

The __init__.py files are required to make Python treat thedirectories as containing packages; this is done to preventdirectories with a common name, such as string, from unintentionallyhiding 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/

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