[英]How to fix ModuleNotFoundError?
I've read a few Python relative vs. absolute import tutorials and can't figure out this ModuleNotFound
error for the life of me. 我读过一些Python相对vs.绝对导入教程,在我的生命中无法弄清楚这个ModuleNotFound
错误。
I'm working with the following directory structure: 我正在使用以下目录结构:
project
|
+-- pseudo
| |
| +-- __main__.py
| |
| +-- pseudo.py
| |
| +-- analytics_generator
| |
| +-- analytics_generator.py
| |
| +-- models
| |
| +-- blueprint.py
The root of the problem is that in the analytics_generator.py file, I'm trying to import SomeClass from blueprint.py. 问题的根源在于,我正在analytics_generator.py文件中尝试从blueprint.py导入SomeClass。
When I execute the main function in __main__.py
, I get the following error: 当我在__main__.py
执行main函数时,出现以下错误:
Traceback (most recent call last):
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1741, in <module>
main()
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1735, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1135, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File ".../project/pseudo/__main__.py", line 2, in <module>
from pseudo import Pseudo
File ".../project/pseudo/pseudo.py", line 4, in <module>
from analytics_generator.analytics_generator import AnalyticsGenerator
File ".../project/pseudo/analytics_generator/analytics_generator.py", line 1, in <module>
from models.blueprints import SomeClass
ModuleNotFoundError: No module named 'models'
I'm running the script within Pycharm and my working directory is .../project/pseudo
我正在Pycharm中运行脚本,我的工作目录是.../project/pseudo
In the analytics_generator.py file, if I change the import statement to a relative import it works: from .models.blueprints import SomeClass
. 在analytics_generator.py文件中,如果我将import语句更改为相对导入,则它可以工作: from .models.blueprints import SomeClass
。
However, using the full path doesn't: 但是,使用完整路径不会:
from pseudo.analytics_generator.models.blueprints import SomeClass
throws: from pseudo.analytics_generator.models.blueprints import SomeClass
引发:
ModuleNotFoundError: No module named 'pseudo.analytics_generator'; 'pseudo' is not a package
Any guidance is much appreciated! 任何指导深表感谢!
The directory in which the script is being executed shouldn't need to be specified. 无需指定执行脚本的目录。 Since you're executing __main__.py
, the following should do: 由于您正在执行__main__.py
,因此应该执行以下操作:
from analytics_generator.models.blueprint import SomeClass
Source/Further Reading: The Definitive Guide to Python import Statements: Absolute vs. Relative Import 资料来源/进一步阅读: Python导入的权威指南语句:绝对与相对导入
Example Directory Structure 示例目录结构
test/ # root folder packA/ # package packA subA/ # subpackage subA __init__.py sa1.py sa2.py __init__.py a1.py a2.py packB/ # package packB (implicit namespace package) b1.py b2.py math.py random.py other.py start.py
For example, suppose we are running
start.py
which importsa1
which in turn importsother
,a2
, andsa1
. 例如,假设我们正在运行start.py
,其中导入了a1
,而后者又导入了other
,a2
和sa1
。 Then the import statements ina1.py
would look as follows: 然后,a1.py
的import语句如下所示:
absolute imports: 绝对进口:
import other import packA.a2 import packA.subA.sa1
Note that there isn't a need to specify import test.other
or import test.packA.test.other
(where test
is the directory in which the script start.py
is being executed). 请注意,不需要指定import test.other
或import test.packA.test.other
(其中test
是执行脚本start.py
的目录)。 The same principle should apply to your situation, regardless of __init__.py
or not , given you're using Python 3.3 and above. 鉴于您使用的是Python 3.3及更高版本, 无论是否使用__init__.py
, 都应遵循相同的原则。
For posterity and completeness, I'll quote another section of the guide: 为了后代和完整性,我将引用指南的另一部分:
[...] when Python runs a script, its containing folder is not considered a package. [...] Python运行脚本时,其包含的文件夹不被视为软件包。
This explains the 'pseudo' is not a package
error. 这说明'pseudo' is not a package
错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.