Python包导入错误导入错误:没有已知父包的相对导入尝试
该项目具有与图片相同的结构:我试图从“index.py”中的“mod.py”导入
from .. import mod
但是,它给出错误:“ImportError:尝试相对导入,没有已知的父包”如果您使用此选项:
from pack1 import mod
然后错误:“ModuleNotFoundError错误:没有名为'pack1'的模块”
PROJECT/
pack1/
__init__.py
mod.py
pack2/
__init__.py
index.py
有什么问题吗?
The project has the same structure as in the picture: I'm trying to import from "mod.py " in "index.py "
from .. import mod
However, it gives the error: "ImportError: attempted relative import with no known parent package" If you use this option:
from pack1 import mod
Then error: "ModuleNotFoundError error: there is no module named 'pack1'"
PROJECT/
pack1/
__init__.py
mod.py
pack2/
__init__.py
index.py
What is the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 StackOverflow 上反复出现的问题。 (在我看来)大部分的困惑来自于 Python 解释它所看到的文件和文件夹的方式是基于 Python 运行的位置。首先,一些术语:
当你在一个目录(文件夹)中启动Python时,它不“知道”该目录的命名空间应该是什么。即,如果您在启动 Python 时在
Z:\path\to_my\project\
中工作:project
是一个包裹。__init__.py
怎么样?从 3.3 版本开始,Python 具有隐式命名空间包,允许导入而无需创建空的 __init__.py 文件。考虑#2:如果你有两个文件:
first.py
和second.py
:包含这些内容:
如果你尝试像这样导入:
Python基本上会执行以下操作:
.first
.
表示获取包含first.py
的包(文件夹)" "first.py
的父包!”相同的规则也适用于 #3。如果我们向项目中添加一些包,如下所示
:以下内容:
当您尝试像这样导入时:
导入
pack2/index.py
也会因为second.py
的原因而失败,Python 会像这样沿着导入链向上移动:..pack1
导入mod
。.
是index.py
的pack2
父包命名空间,发现了。”..
是pack2
的父包。”pack2
的父包!”我们如何让它发挥作用?两件事。
首先,将 Python 的运行位置上移一级,以便所有 .py 文件和子文件夹都被视为同一包命名空间的一部分,这允许文件使用相对引用相互引用。
因此,现在 Python 将
project
视为包命名空间,并且其中的所有文件都可以使用该级别的相对引用。这会改变您在 Python 解释器中导入的方式:
其次,您可以进行显式引用而不是相对引用。这可能会使导入语句变得非常长,但如果您有几个需要相互拉取的顶级模块,那么您可以这样做。当您在一个文件中定义函数并在另一个文件中编写脚本时,这非常有用。
我希望这有助于消除有关相对进口的一些困惑。
This is a recurring question on StackOverflow. And much of the confusion (in my opinion) comes from how Python interprets the files and folders it sees is based on where Python is run from. First, some terminology:
When you start Python in a directory (folder), it doesn't "know" what the namespace of that directory should be. I.e., if you are working in
Z:\path\to_my\project\
when you start Python:project
to be a package.__init__.py
? Since version 3.3, Python has implicit namespace packages, which allows importing without needing to create an empty__init__.py
file.Consider #2: if you have two files:
first.py
andsecond.py
:with these contents:
if you try to import like this:
Python basically does the following:
.first
.
means get the package (folder) that containsfirst.py
"first.py
!"The same rules apply for #3 as well. If we add a few packages to the project like this:
with the following contents:
and when you try to import like this:
The import in
pack2/index.py
is going to fail for the same reasonsecond.py
, Python will work its way up the import chain of dots like this:index.py
as a module."mod
from..pack1
..
is thepack2
parent package namespace ofindex.py
, found that."..
is the parent package ofpack2
."pack2
!"How do we make it work? Two thing.
First, move where Python is running up one level so that all of the .py files and subfolders are considered to be part of the same package namespace, which allows the file to reference each other using relative references.
So now Python sees
project
as a package namespace, and all of the files within can use relative references up to that level.This changes how you import when you are in the Python interpreter:
Second, you make explicit references instead of relative references. That can make the import statements really long, but if you have several top-level modules that need to pull from one another, this is how you can do it. This is useful when you are defining your functions in one file and writing your script in another.
I hope this helps clear up some of the confusion about relative imports.