从__main__.py启动flask服务器

时间:2016-12-09 21:04:05

标签: python python-3.x flask wsgi

我正在尝试通过运行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

的最简洁方法

0 个答案:

没有答案