'ModuleNotFoundError' 尝试从导入的包中导入模块时
'ModuleNotFoundError' when trying to import module from imported package
这是我的目录结构:
man/
Mans/
man1.py
MansTest/
SoftLib/
Soft/
SoftWork/
manModules.py
Unittests/
man1test.py
man1.py
包含以下 import 语句,我不想更改:
from Soft.SoftWork.manModules import *
man1test.py
包含以下 import 语句:
from ...MansTest.SoftLib import Soft
from ...Mans import man1
我需要 man1test.py
中的第二个 import 因为 man1test.py
需要访问 man1.py
.
中的函数
我第一次导入 (Soft) 的基本原理是为了方便前面提到的 man1.py
中的 import 语句。
然而,与我的预期相反,man1.py
中的 import 语句导致:
ModuleNotFoundError: No module named 'Soft'
当我运行
python3 -m man.MansTest.Unittests.man1test
来自 man/.
之上的目录
有什么方法可以解决这个错误,而不需要更改 man1.py
和 中的 import 语句而不向 [ 添加任何内容=46=]sys.path?
编辑:python3 -m man.ManTest.Unittests.man1test
由原版问题改为python3 -m man.MansTest.Unittests.man1test
FIRST,如果你希望能够从[=98=访问man1.py ] AND manModules.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
SECOND,针对man1test.py中from ...Mans import man1
导致的“ModuleNotFoundError: No module named 'Soft'
”错误,记录的解决方案是将 man1.py 添加到 sys.path
因为 Mans 在 之外MansTest 包。请参阅 Python 文档中的 The Module Search Path。但是如果不想直接修改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.
THIRD,对于 from ...MansTest.SoftLib import Soft
,您说“ 是为了促进上述 man1.py 中的导入语句",这就是现在导入的方式。如果要在 man1.py 中导入 Soft.SoftLib,则必须设置 man1.py 找到 Soft.SoftLib 并直接导入。
话虽如此,这就是我如何让它发挥作用的。
man1.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()
manModules.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 文件时,我在导入过程中遇到了这个错误。
我必须创建一个类型为“.py”的文件名,例如 filename.py,然后将其保存为 python 文件。
post 尝试导入对我有用的文件。
有些包名和模块名可能相同。此冲突也以此错误响应。
我遇到了类似的问题,虽然不一样。
我的文件夹和文件的结构(GUI_ML 是主文件夹):
GUI_ML
\ Views \ login.py
\ Views \ __ init __.py
\ Controllers \ Control_login.py
\ Controllers \ __ init __.py
我需要从 login.py 导入 Control_login.py。以下代码解决了我的问题:
import sys
import os
myDir = os.getcwd()
sys.path.append(myDir)
from pathlib import Path
path = Path(myDir)
a=str(path.parent.absolute())
sys.path.append(a)
from Controllers.Control_login import Control_login
我遇到了类似的问题,我忘记在模块中添加 __init__.py
并且花了很多时间来尝试理解问题所在。
这是我的目录结构:
man/
Mans/
man1.py
MansTest/
SoftLib/
Soft/
SoftWork/
manModules.py
Unittests/
man1test.py
man1.py
包含以下 import 语句,我不想更改:
from Soft.SoftWork.manModules import *
man1test.py
包含以下 import 语句:
from ...MansTest.SoftLib import Soft
from ...Mans import man1
我需要 man1test.py
中的第二个 import 因为 man1test.py
需要访问 man1.py
.
我第一次导入 (Soft) 的基本原理是为了方便前面提到的 man1.py
中的 import 语句。
然而,与我的预期相反,man1.py
中的 import 语句导致:
ModuleNotFoundError: No module named 'Soft'
当我运行
python3 -m man.MansTest.Unittests.man1test
来自 man/.
之上的目录有什么方法可以解决这个错误,而不需要更改 man1.py
和 中的 import 语句而不向 [ 添加任何内容=46=]sys.path?
编辑:python3 -m man.ManTest.Unittests.man1test
由原版问题改为python3 -m man.MansTest.Unittests.man1test
FIRST,如果你希望能够从[=98=访问man1.py ] AND manModules.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 namedB
in a package namedA
....
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 asstring
, 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
SECOND,针对man1test.py中from ...Mans import man1
导致的“ModuleNotFoundError: No module named 'Soft'
”错误,记录的解决方案是将 man1.py 添加到 sys.path
因为 Mans 在 之外MansTest 包。请参阅 Python 文档中的 The Module Search Path。但是如果不想直接修改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 variablePATH
).- The installation-dependent default.
THIRD,对于 from ...MansTest.SoftLib import Soft
,您说“ 是为了促进上述 man1.py 中的导入语句",这就是现在导入的方式。如果要在 man1.py 中导入 Soft.SoftLib,则必须设置 man1.py 找到 Soft.SoftLib 并直接导入。
话虽如此,这就是我如何让它发挥作用的。
man1.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()
manModules.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 文件时,我在导入过程中遇到了这个错误。 我必须创建一个类型为“.py”的文件名,例如 filename.py,然后将其保存为 python 文件。 post 尝试导入对我有用的文件。
有些包名和模块名可能相同。此冲突也以此错误响应。
我遇到了类似的问题,虽然不一样。
我的文件夹和文件的结构(GUI_ML 是主文件夹):
GUI_ML
\ Views \ login.py
\ Views \ __ init __.py
\ Controllers \ Control_login.py
\ Controllers \ __ init __.py
我需要从 login.py 导入 Control_login.py。以下代码解决了我的问题:
import sys
import os
myDir = os.getcwd()
sys.path.append(myDir)
from pathlib import Path
path = Path(myDir)
a=str(path.parent.absolute())
sys.path.append(a)
from Controllers.Control_login import Control_login
我遇到了类似的问题,我忘记在模块中添加 __init__.py
并且花了很多时间来尝试理解问题所在。