假设我有这样的结构:
dir/
__init__.py
dir1/
__init__.py
x.py
dir2/
__init__.py
y.py
现在我想将 x.py 导入到 y.py 。
我在 y.py 中尝试这个 from ..dir1.x import * from PEP 328但我收到此错误 Attempted relative import in non-package .
我搜索了几个小时,但找不到这个问题的任何答案。
有很多类似我的问题,但没有一个像this一样帮助我
请帮助。
非常感谢。
请您参考如下方法:
在y.py中加入这段导入代码
import sys
sys.path.insert(0, '..')
然后做
from dir1.x import *
