简体   繁体   English

为什么 app.run 与调试模式不工作?

[英]Why is app.run with debug mode on not working?

I tried to start a Flask application with app.run() in python shell. When I passed debug=False, it worked, but not working with debug=True which gave me the following python error:我尝试在 python shell 中使用 app.run() 启动一个 Flask 应用程序。当我通过 debug=False 时,它起作用了,但没有使用 debug=True,这给了我以下 python 错误:

>>> from HelloW import app
>>> app.run(debug=True, port=1234)
 * Serving Flask app 'HelloW' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with stat
C:\Users\lfcta\Documents\Python\python.exe: can't find '__main__' module in 'E:\\Flask\\Flask_WTF'

where E:\Flask\Flask_WTF\Hellow.py contains the following code:其中 E:\Flask\Flask_WTF\Hellow.py 包含以下代码:

from    flask   import Flask

app = Flask(__name__)

@app.route('/home')
def home():
    return '<h2>Hello World!</h2>'

I don't encounter this problem with the "flask run" command regardless of the debug mode.无论调试模式如何,“flask run”命令都不会遇到这个问题。

E:\Flask\Flask_WTF>set flask_app=HelloW.py

E:\Flask\Flask_WTF>set flask_debug=1
E:\Flask\Flask_WTF>flask run // working

E:\Flask\Flask_WTF>set flask_debug=0
E:\Flask\Flask_WTF>flask run // working
 

The problem only occurs when you run the code from an interactive shell. It is caused by a feature in werkzeug (the wsgi server flask is based on).只有当您从交互式 shell 运行代码时才会出现此问题。它是由werkzeug中的一个功能引起的(wsgi 服务器 flask 基于)。

In debug mode werkzeug will automatically restart your server if a project file is changed.在调试模式下,如果项目文件发生更改,werkzeug 将自动重启您的服务器。 Everytime a change is detected werkzeug restarts the file that was initially started.每次检测到更改时,werkzeug 都会重新启动最初启动的文件。 Even the first start is done via the file name!即使是第一次启动也是通过文件名完成的!

But in the interactive shell there is no file at all and werkzeug thinks your file is "" (empty string).但是在交互式 shell 中根本没有文件,werkzeug 认为你的文件是“”(空字符串)。 It then tries to run that file.然后它会尝试运行该文件。 For some reason it also thinks that the "" refers to a package. But since that package does not exist it also cannot have a main module, hence the error.出于某种原因,它还认为“”指的是 package。但由于 package 不存在,它也不能有模块,因此出现错误。

You can simulate that error by running "" directly您可以通过直接运行“”来模拟该错误

python "" python ""

prints: can't find '__main__' module in ''

You could try to disable the reloader by setting debug to False (which is also the default):您可以尝试通过将 debug 设置为 False(这也是默认设置)来禁用重新加载程序:

app.run(debug=False, ...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM