python - Flask autoreload 不重新加载或拾取更改

标签 python flask

我有一个 app.py flask 应用程序,我想为其启用自动重新加载。这是入口点:

APP = Flask(__name__)
APP.config.from_object(os.environ['APP_SETTINGS'])
# a lot of configurations ommited
if __name__ == "__main__":
    APP.run(debug=True, port=5005)

当我运行应用程序时,我在终端中得到了这个:

/Users/George/myproject/venv/bin/python /Users/George/myproject/app.py
 * Running on http://127.0.0.1:5005/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger pin code: 338-825-330

当我修改我的一个 Controller 时,它发现发生了变化:

 * Detected change in '/Users/George/myproject/web_endpoints/user.py', reloading

但更改不会发生,如果我再做一次更改,它永远不会被拾取(不会在终端中报告)。

最佳答案

不建议 Flask 将 app.run() 与自动重新加载一起使用,因为它受到严重支持

这是Flask源码中的注释

def run(self, host=None, port=None, debug=None, load_dotenv=True, **options):

    """Runs the application on a local development server.
    ...        

    If you want to run the application in debug mode, but disable the
    code execution on the interactive debugger, you can pass
    ``use_evalex=False`` as parameter.  This will keep the debugger's
    traceback screen active, but disable code execution.


    It is not recommended to use this function for development with
    automatic reloading as this is badly supported.  Instead you should
    be using the :command:`flask` command line script's ``run`` support.

    ...  
    """
    pass      

但是你可以像这样强制使用重新加载器:

if __name__ == '__main__':
    app.run(host=config.HOST, port=config.PORT, debug=True)

注意,app.run() 必须用 if __name__ == '__main__' 包裹。

关于python - Flask autoreload 不重新加载或拾取更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45089465/

相关文章:

python - 如果没有错误就给变量赋值

python - 使用 RTMP 协议(protocol)将视频文件流式传输到使用 Python 的 rtmp 服务器

Python 链表实现和对象建模

python - 在Python中插入缺失值

Python 请求 - 客户端证书的 SSL 错误

python - 如何在 AWS Elastic Beanstalk 中使用 Plotly Python SDK

Flask Migrate 使用不同的 postgres 模式( __table_args__ = {'schema' : 'test_schema' ]})

python - Flask 构建错误

Python(Flask + Swagger)Flasgger 抛出 404 错误

docker - 如何让 Flask 为 Alpine 上的静态文件提供正确的内容类型?