我正在尝试通过运行python -m my_app
内部my_app/__main__.py
我有:
from .main import app
app.run()
然而,在运行python -m my_app
时,我得到了:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
Traceback (most recent call last):
File "/my_app/__main__.py", line 2, in <module>
from .main import app
SystemError: Parent module '' not loaded, cannot perform relative import
表示相对导入存在问题,但奇怪的是烧瓶应用程序似乎试图运行。从逻辑上讲,接下来我删除了相对导入:
from my_app.main import app
这导致:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
Traceback (most recent call last):
File "/my_app/__main__.py", line 2, in <module>
from my_app.main import app
ImportError: No module named 'my_app'
向__main__.py
print('1.')
from store_app.main import app
print('2.')
app.run()
print('3.')
当刻录机重新加载__main__.py
1.
2.
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
1.
Traceback (most recent call last):
File "/my_app/__main__.py", line 3, in <module>
from my_app.main import app
ImportError: No module named 'my_app'
这表明,如果没有将模块添加到__main__.py
,则会重新加载sys.path
。有没有人为此工作?或者是将模块手动插入sys.path