Flask at first run: Do not use the development server in a production environment

Question:

I installed the Flask plugin in PyCharm Community Edition and I just have this simple code in my flask app:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
    return '<h1>Hello!</h1>'

if __name__ == "__main__":
    app.run(debug=True)

And I get this message:

WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead

* Restarting with stat
* Debugger is active!
* Debugger PIN: 123-456-789
* Running on http://127.0.0.1:5000/

Why am I getting this error when I run Flask?


A previous version of the message read “Do not use the development server in a production environment.”

Asked By: Anatoly

||

Answers:

As of Flask 2.2, the development server always shows this warning, it is not possible to disable it. The development server is not intended for use in production. It is not designed to be particularly efficient, stable, or secure. Use a production WSGI server instead. See the deployment docs from Flask for more information.

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.

That warning applies to the development server, not Flask itself. The Flask framework is appropriate for any type of application and deployment.

Answered By: davidism

For deploying an application to production, one option is to use Waitress, a production WSGI server.

Here is an example of using waitress in the code.

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)

Running the application:

$ python hello.py

Waitress also provides a command line utility waitress-serve. To use that, you can modify the code to the following:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def index():
    return "<h1>Hello!</h1>"

def create_app():
   return app

Then we can use waitress-serve as the following:

waitress-serve --port=8080 --call hello:create_app

And BTW, 8080 is the default port.

To validate the deployment, open a separate window:

% curl localhost:8080
<h1>Hello!</h1>%                     

Or directly in your browser http://localhost:8080/.


Other alternatives to deploy your app include Gunicorn and uWSGI. For more details, please refer to the flask deploy doc.

Answered By: Yuchen

If for some people (like me earlier) the above answers don’t work, I think the following answer would work (for Mac users I think)
Enter the following commands to do flask run

$ export FLASK_APP=hello.py
$ export FLASK_ENV=development
$ flask run

Alternatively you can do the following (I haven’t tried this but one resource online talks about it)

$ export FLASK_APP=hello.py
$ python -m flask run

source: For more

Answered By: quickLambda

To avoid these messsages, inside the CLI (Command Line Interface), run these commands.

export FLASK_APP=app.py
export FLASK_ENV=development
export FLASK_DEBUG=0
flask run
Answered By: Omar Magdy

This worked for me on windows:

$env:FLASK_APP="flask_project.py"
$env:FLASK_ENV="development"
flask run

flask_project.py is on the same path as my virtual environment.

Answered By: Edgar A.Franco

Try gevent:

from flask import Flask
from gevent.pywsgi import WSGIServer

app = Flask(__name__)

@app.route('/api', methods=['GET'])
def index():
    return "Hello, World!"

if __name__ == '__main__':
    # Debug/Development
    # app.run(debug=True, host="0.0.0.0", port="5000")
    # Production
    http_server = WSGIServer(('', 5000), app)
    http_server.serve_forever()

Note: Install gevent using pip install gevent

Answered By: Abolfazl Rastgou
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.