apache - Auto reloading python Flask app upon code changes

ID : 10208

viewed : 60

Tags : pythonapachewebflaskpython

Top 5 Answer for apache - Auto reloading python Flask app upon code changes

vote vote


The current recommended way is with the flask command line utility.



$ 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 --port option.


$ FLASK_APP=main.py FLASK_ENV=development flask run --port 8080 

More options are available with:

$ flask run --help 

FLASK_APP can also be set to module:app or module:create_app instead of module.py. See https://flask.palletsprojects.com/en/1.1.x/cli/#application-discovery for a full explanation.

vote vote


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.


Or, from the shell:

$ export FLASK_DEBUG=1 $ flask run 


vote vote


In test/development environments

The werkzeug debugger already has an 'auto reload' function available that can be enabled by doing one of the following:



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;


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-reload option allows you to specify a file/folder that will cause the uWSGI application to reload your newly deployed flask app.

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:

touch-reload = '/opt/virtual_environments/application/reload_me.txt' 

I hope this helps!

vote vote


If you're running using uwsgi look at the python auto reload option:

uwsgi --py-autoreload 1 

Example uwsgi-dev-example.ini:

[uwsgi] socket = 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 


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.

vote vote


A few updates for Flask 1.0 and above

the basic approach to hot re-loading is:

$ export FLASK_APP=my_application $ export FLASK_ENV=development $ flask run 
  • you should use FLASK_ENV=development (not FLASK_DEBUG=1)
  • as a safety check, you can run flask run --debugger just to make sure it's turned on
  • the Flask CLI will now automatically read things like FLASK_APP and FLASK_ENV if you have an .env file in the project root and have python-dotenv installed

Top 3 video Explaining apache - Auto reloading python Flask app upon code changes