还有许多其他问题和答案解释了Python绝对导入的细微差别。从这些,我有一个在Linux上运行良好的大型项目。但是要使Windows可执行文件,必须在Windows上编译它,并且在此平台上绝对引用似乎会中断。我认为问题是环境问题,但我不知道为什么。
# I've tried both setting and unsetting the python path to the project,# which then shows up in > python -c "import sys; print(sys.path)"
> $env:PYTHONPATH = "C:\Users\Me\Workspace\my_proj"
# I've tried both with and without an __init__.py file in Workspace\
制作一个玩具项目来调试它:
my_proj\
目录包含__init__.py
,a.py
和b.py
如果a.py
包含行def foo(): print('Awesome')
,那么对于b.py
,我可以将其导入为import a
没问题:import a; a.foo()
,在任何目录中,都没有__init__.py
。
但是要使用import my_proj.a
或from my_proj import a
,从my_proj\
或Workspace\
目录中调用带有或不带有__init__.py
文件的目录都无效。
如果python在python path / sys.path上可以看到目录my_proj\
,那为什么不起作用?