Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在y.py,添加这段导入代码
y.py
import sys sys.path.insert(0, '..')
然后做
from dir1.x import *
直接调用文件时,相对导入将不起作用:
python y.py
因为他们有__name__ == '__main__'而不是完整的包名。
__name__ == '__main__'
要使相对导入起作用,您必须将y其用作包:
y
python -m dir.dir2.y