我有以下源代码结构
testapp/
├─ __init__.py
├─ testmsg.py
├─ sub/
│ ├─ __init__.py
│ ├─ testprinter.py
testmsg
定义了以下常量:
MSG = "Test message"
和 sub/testprinter.py
:
import testmsg
print("The message is: {0}".format(testmsg.MSG))
但是我得到了 ImportError: No module named testmsg
的错误。
既然按照包的结构应该可以工作,那么我不想在每个子模块中都扩展sys.path,也不想使用相对导入。
我在这里做错了什么?
python sub/testprinter.py
,但是在sub
目录中运行python testprinter.py
也不起作用。 - user1543863