Infrorror:如何从父目录导入功能或类
我正在尝试将类从父目录导入到我的脚本中,但是我接收未知的父母软件包
错误的尝试相对导入,我已经在Internet中到处搜索,但仍然无法解决错误。这是我的软件包结构:
A/
a.py
__init__.py
B/
__init__.py
c.py
现在假设我有 class1 内部a.py
模块,我正在尝试在c.py
模块中导入它。 。我已经尝试过:
from ..A.a import class1
但是我收到了上述错误消息,我尝试将一个文件夹添加到sys.path
,但仍然存在相同的错误。任何人都可以解释如何从函数或类导入parent Directory到子目录的软件包(例如a.py
c.py
)
I am trying to import a class from parent directory to my script but I receive attempted relative import with no known parent package
error, I have searched everywhere in the internet but still cannot fix the error. Here is my package structure:
A/
a.py
__init__.py
B/
__init__.py
c.py
Now assume I have class1 inside a.py
module and I am trying to import it in the c.py
module. I have tried:
from ..A.a import class1
But i get above error message I have tried to add the A folder to sys.path
but still the same error. Can anyone explain how I can import a package from a function or class from parent directory to subdirectory (like from a.py
to c.py
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我这样做的方式是通过附加来扩展
c.py
文件中的路径。以下是一个示例:a.py (expent the parent Directory中的示例模块)
文件中的a.py在父目录中)
c.py (示例 您的
..
无效的是,以开头的导入。它)。使用您的示例的一个示例是将
可以从d.py
与a.py
一起添加,该a.py
通过使用.d Import *使用。
The way I do this is my extending the path in the
c.py
file by appending it. Below is an example of this:a.py (example module in the parent directory)
c.py (example file invoking a.py in the parent directory)
The reason why your
..
didn't work is because imports starting with.
or..
are for importing from within things being imported (the way I understand it). An example of this to use your example would be to add ad.py
alongsidea.py
which could be imported froma.py
by usingfrom .d import *
.