Skip to main content
 首页 » 编程设计

python之FLASK_ENV 好像被忽略了(进不去调试环境)

2025年05月04日18txw1958

根据 Flask 文档,

The flask script is nice to start a local development server, but you would have to restart it manually after each change to your code. That is not very nice and Flask can do better. If you enable debug support the server will reload itself on code changes, and it will also provide you with a helpful debugger if things go wrong.

To enable all development features (including debug mode) you can export the FLASK_ENV environment variable and set it to development before running the server:


$ export FLASK_ENV=development  
$ flask run 

但是,在我的非常简单的示例中,代码更改在我重新启动服务器之前仍然不会生效。我已经设置了我想要运行的特定脚本 export FLASK_APP=hello.py脚本如下:
from flask import Flask, url_for, request, render_template 
app = Flask(__name__) 
 
@app.route("/") 
def hello(): 
    return "Hello World!!" 

在 Flask 开发服务器运行时,我通过添加或删除感叹号并保存文件来更改返回值。然后我刷新页面 http://127.0.0.1:5000/在 Chrome 中,但感叹号的数量没有变化。一旦我在终端中使用 Ctrl-C 退出 Flask并重新启动它然后再次刷新页面,我得到了正确数量的感叹号。

这是在 Mac、Python 3.6.0 (Anaconda)、Flask 0.12 上。

我是否误解了开发服务器如何帮助我,或者您认为我应该检查其他什么?我对 Flask 很陌生。

请您参考如下方法:

尝试

FLASK_APP=app.py FLASK_DEBUG=1 TEMPLATES_AUTO_RELOAD=1 flask run 
FLASK_DEBUG会让你得到你现在正在寻找的行为; TEMPLATE_AUTO_RELOAD一旦您开始使用模板,就会为您提供所需的行为。