代码更改时自动重新加载 python Flask 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16344756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Auto reloading python Flask app upon code changes
提问by Passiday
I'm investigating how to develop a decent web app with Python. Since I don't want some high-order structures to get in my way, my choice fell on the lightweight Flask framework. Time will tell if this was the right choice.
我正在研究如何使用 Python 开发一个不错的 Web 应用程序。由于我不希望一些高阶结构妨碍我,我的选择落在了轻量级Flask 框架上。时间会证明这是否是正确的选择。
So, now I've set up an Apache server with mod_wsgi, and my test site is running fine. However, I'd like to speed up the development routine by making the site automatically reload upon any changes in py or template files I make. I see that any changes in site's .wsgi file causes reloading (even without WSGIScriptReloading On in the apache config file), but I still have to prod it manually (ie, insert extra linebreak, save). Is there some way how to cause reload when I edit some of the app's py files? Or, I am expected to use IDE that refreshes the .wsgi file for me?
所以,现在我已经使用 mod_wsgi 设置了一个 Apache 服务器,并且我的测试站点运行良好。但是,我想通过使站点在我所做的 py 或模板文件中的任何更改时自动重新加载来加快开发程序。我看到站点的 .wsgi 文件中的任何更改都会导致重新加载(即使在 apache 配置文件中没有 WSGIScriptReloading On),但我仍然必须手动激活它(即,插入额外的换行符,保存)。当我编辑一些应用程序的 py 文件时,有什么方法可以导致重新加载?或者,我应该使用 IDE 为我刷新 .wsgi 文件?
采纳答案by Eyal Levin
The current recommended way is with the flaskcommand line utility.
当前推荐的方法是使用flask命令行实用程序。
https://flask.palletsprojects.com/en/1.1.x/quickstart/#debug-mode
https://flask.palletsprojects.com/en/1.1.x/quickstart/#debug-mode
Example:
例子:
$ export FLASK_APP=main.py
$ export FLASK_ENV=development
$ flask run
or in one command:
或在一个命令中:
$ FLASK_APP=main.py FLASK_ENV=development flask run
If you want different port than the default (5000) add --portoption.
如果您想要与默认 ( 5000)不同的端口,请添加--port选项。
Example:
例子:
$ FLASK_APP=main.py FLASK_ENV=development flask run --port 8080
More options are available with:
更多选项可用:
$ flask run --help
回答by codegeek
If you are talking about test/dev environments, then just use the debug option. It will auto-reload the flask app when a code change happens.
如果您在谈论测试/开发环境,那么只需使用调试选项。当发生代码更改时,它会自动重新加载 Flask 应用程序。
app.run(debug=True)
Or, from the shell:
或者,从外壳:
$ export FLASK_DEBUG=1
$ flask run
回答by Ewan
In test/development environments
在测试/开发环境中
The werkzeug debugger already has an 'auto reload' function available that can be enabled by doing one of the following:
werkzeug 调试器已经有一个可用的“自动重新加载”功能,可以通过执行以下操作之一来启用它:
app.run(debug=True)
or
或者
app.debug = True
You can also use a separate configuration file to manage all your setup if you need be. For example I use 'settings.py' with a 'DEBUG = True' option. Importing this file is easy too;
如果需要,您还可以使用单独的配置文件来管理所有设置。例如,我使用带有 'DEBUG = True' 选项的 'settings.py'。导入这个文件也很容易;
app.config.from_object('application.settings')
However this is not suitable for a production environment.
但是,这不适用于生产环境。
Production environment
生产环境
Personally I chose Nginx + uWSGI over Apache + mod_wsgi for a few performance reasons but also the configuration options. The touch-reloadoption allows you to specify a file/folder that will cause the uWSGI application to reload your newly deployed flask app.
我个人选择 Nginx + uWSGI 而不是 Apache + mod_wsgi 出于一些性能原因以及配置选项。该触摸重装选项允许你指定一个文件/文件夹会导致uWSGI应用程序重新加载新部署的烧瓶应用。
For example, your update script pulls your newest changes down and touches 'reload_me.txt' file. Your uWSGI ini script (which is kept up by Supervisord - obviously) has this line in it somewhere:
例如,您的更新脚本会拉取最新的更改并访问“reload_me.txt”文件。您的 uWSGI ini 脚本(由 Supervisord 维护 - 显然)在某处包含以下行:
touch-reload = '/opt/virtual_environments/application/reload_me.txt'
I hope this helps!
我希望这有帮助!
回答by Kyle James Walker
If you're running using uwsgi look at the python auto reload option:
如果您使用 uwsgi 运行,请查看 python 自动重新加载选项:
uwsgi --py-autoreload 1
Example uwsgi-dev-example.ini:
示例 uwsgi-dev-example.ini:
[uwsgi]
socket = 127.0.0.1:5000
master = true
virtualenv = /Users/xxxx/.virtualenvs/sites_env
chdir = /Users/xxx/site_root
module = site_module:register_debug_server()
callable = app
uid = myuser
chmod-socket = 660
log-date = true
workers = 1
py-autoreload = 1
site_root/__init__.py
site_root/__init__.py
def register_debug_server():
from werkzeug.debug import DebuggedApplication
app = Flask(__name__)
app.debug = True
app = DebuggedApplication(app, evalex=True)
return app
Then run:
然后运行:
uwsgi --ini uwsgi-dev-example.ini
Note: This example also enables the debugger.
注意:此示例还启用了调试器。
I went this route to mimic production as close as possible with my nginx setup. Simply running the flask app with it's built in web server behind nginx it would result in a bad gateway error.
我走这条路线是通过我的 nginx 设置尽可能地模拟生产。简单地运行带有 nginx 后面的内置 Web 服务器的 Flask 应用程序,它会导致错误的网关错误。
回答by blue-sky
To achieve this in PyCharm set 'Environment Variables' section to:
为了在 PyCharm 中实现这一点,将“环境变量”部分设置为:
PYTHONUNBUFFERED=1;
FLASK_DEBUG=1
For Flask 'run / debug configurations'.
对于 Flask '运行/调试配置'。
回答by Zach Valenta
A few updates for Flask 1.0
Flask 1.0 的一些更新
basic approach to hot re-loading is:
热重载的基本方法是:
$ export FLASK_APP=my_application
$ export FLASK_ENV=development
$ flask run
- you should use
FLASK_ENV=development(notFLASK_DEBUG=1) - as a safety check, you can run
flask run --debuggerjust to make sure it's turned on - the Flask CLI will now automatically read things like
FLASK_APPandFLASK_ENVif you have an.envfile in the project rootand have python-dotenv installed - here is a working link to the docs(accepted answer's link is broken)
- 你应该使用
FLASK_ENV=development(不是FLASK_DEBUG=1) - 作为安全检查,您可以运行
flask run --debugger以确保它已打开 - 该瓶CLI现在会自动读取之类的东西
FLASK_APP,并FLASK_ENV如果你有一个.env项目的根文件,并已经安装了Python,dotenv - 这是文档的工作链接(已接受答案的链接已损坏)
回答by Anthonyeef
I got a different idea:
我有了不同的想法:
First:
第一的:
pip install python-dotenv
Install the python-dotenvmodule, which will read local preference for your project environment.
安装python-dotenv模块,它将读取您的项目环境的本地首选项。
Second:
第二:
Add .flaskenvfile in your project directory. Add following code:
.flaskenv在您的项目目录中添加文件。添加以下代码:
FLASK_ENV=development
It's done!
完成!
With this config for your Flask project, when you run flask runand you will see this output in your terminal:
使用 Flask 项目的此配置,当您运行时flask run,您将在终端中看到此输出:
And when you edit your file, just save the change. You will see auto-reload is there for you:
当您编辑文件时,只需保存更改即可。你会看到自动重新加载是为你准备的:
With more explanation:
更多解释:
Of course you can manually hit export FLASK_ENV=developmentevery time you need. But using different configuration file to handle the actual working environment seems like a better solution, so I strongly recommend this method I use.
当然,您可以在export FLASK_ENV=development每次需要时手动点击。但是使用不同的配置文件来处理实际的工作环境似乎是一个更好的解决方案,所以我强烈推荐我使用的这种方法。
回答by Asim Fakhi
Flask applications can optionally be executed in debug mode. In this mode, two very convenient modules of the development server called the reloaderand the debuggerare enabled by default. When the reloader is enabled, Flask watches all the source code files of your project and automatically restarts the server when any of the files are modified.
Flask 应用程序可以选择在调试模式下执行。在这种模式下, 默认启用了两个非常方便的开发服务器模块,称为重新加载器和调试器。启用重载器后,Flask 会监视项目的所有源代码文件,并在修改任何文件时自动重新启动服务器。
By default, debug mode is disabled. To enable it, set a FLASK_DEBUG=1 environment variable before invoking flask run:
默认情况下,调试模式处于禁用状态。要启用它,请在调用 flask run 之前设置 FLASK_DEBUG=1 环境变量:
(venv) $ export FLASK_APP=hello.py for Windows use >set FLASK_APP=hello.py
(venv) $ export FLASK_APP=hello.py for Windows 使用 >set FLASK_APP=hello.py
(venv) $ export FLASK_DEBUG=1 for Windows use >set FLASK_DEBUG=1
(venv) $ export FLASK_DEBUG=1 Windows 使用 >set FLASK_DEBUG=1
(venv) $ flask run
(venv) $烧瓶运行
- Serving Flask app "hello"
- Forcing debug mode on
- Running on http://127.0.0.1:5000/(Press CTRL+C to quit)
- Restarting with stat
- Debugger is active!
- Debugger PIN: 273-181-528
- 服务 Flask 应用程序“你好”
- 强制开启调试模式
- 在http://127.0.0.1:5000/ 上运行(按 CTRL+C 退出)
- 用 stat 重新启动
- 调试器处于活动状态!
- 调试器 PIN:273-181-528
Having a server running with the reloaderenabled is extremely useful during development, because every time you modify and save a source file, the server automatically restarts and picks up the change.
在开发过程中,在启用重新加载器的情况下运行服务器非常有用,因为每次修改和保存源文件时,服务器都会自动重新启动并接受更改。


