【发布时间】:2012-07-08 18:13:37
【问题描述】:
我有以下源代码结构
/testapp/
/testapp/__init__.py
/testapp/testmsg.py
/testapp/sub/
/testapp/sub/__init__.py
/testapp/sub/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,我什至不想使用相对导入。
我在这里做错了什么?
【问题讨论】:
-
@SimeonVisser 运行
python sub/testprinter.py,但在sub目录中也运行python testprinter.py不起作用。
标签: python import package importerror