Flask at first run: Do not use the development server in a production environment
The official tutorial discusses deploying an app to production. One option is to use Waitress, a production WSGI server. Other servers include Gunicorn and uWSGI.
When running publicly rather than in development, you should not use the built-in development server (
flask run
). The development server is provided by Werkzeug for convenience, but is not designed to be particularly efficient, stable, or secure.Instead, use a production WSGI server. For example, to use Waitress, first install it in the virtual environment:
$ pip install waitress
You need to tell Waitress about your application, but it doesn’t use
FLASK_APP
like flask run does. You need to tell it to import and call the application factory to get an application object.$ waitress-serve --call 'flaskr:create_app' Serving on http://0.0.0.0:8080
Or you can use waitress.serve()
in the code instead of using the CLI command.
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "<h1>Hello!</h1>"
if __name__ == "__main__":
from waitress import serve
serve(app, host="0.0.0.0", port=8080)
$ python hello.py
Unless you tell the development server that it's running in development mode, it will assume you're using it in production and warn you not to. The development server is not intended for use in production. It is not designed to be particularly efficient, stable, or secure.
Enable development mode by setting the FLASK_ENV
environment variable to development
.
$ export FLASK_APP=example
$ export FLASK_ENV=development
$ flask run
If you're running in PyCharm (or probably any other IDE) you can set environment variables in the run configuration.
Development mode enables the debugger and reloader by default. If you don't want these, pass --no-debugger
or --no-reloader
to the run
command.
That warning is just a warning though, it's not an error preventing your app from running. If your app isn't working, there's something else wrong with your code.