如何组织python导入,以便可以有这样的目录。
project
| \
| __init__.py
|
src
| \
| __init__.py
| classes.py
|
test
\
__init__.py
tests.py
然后inside/project/test/tests.py可以导入classes.py
我在tests.py中有这样的代码
from .. src.classes import(
scheduler
db
)
我犯了错误
SystemError: Parent module '' not loaded, cannot perform relative import
有人知道该怎么办吗?
Python将包含启动脚本的文件夹添加到PYTHONPATH中,因此如果运行
只有文件夹
test
被添加到路径中(而不是在其中执行命令的基本目录)。而是像这样运行测试:
这将把base dir添加到python路径,然后通过非相对导入访问类:
如果您真的想使用相对导入样式,那么您的3个dir需要添加到包目录中
从package dir上面执行
另见:
相关问题 更多 >
编程相关推荐