69

While I am running Flask code from my command line, a warning is appearing:

Serving Flask app "hello_flask" (lazy loading)
* Environment: production
  WARNING: Do not use the development server in a production environment.
  Use a production WSGI server instead.

What does this mean?

IonicSolutions
  • 2,559
  • 1
  • 18
  • 31
Harshit Satya
  • 841
  • 2
  • 10
  • 10
  • 1
    For those running the development server locally, looking to squash that message, you can simply [set `export FLASK_ENV="development"` in an `.env` file](http://flask.pocoo.org/docs/dev/config/#environment-and-debug-features). – Hartley Brody Mar 12 '19 at 20:12

13 Answers13

53

As stated in the Flask documentation:

While lightweight and easy to use, Flask’s built-in server is not suitable for production as it doesn’t scale well and by default serves only one request at a time.

Given that a web application is expected to handle multiple concurrent requests from multiple users, Flask is warning you that the development server will not do this (by default). It recommends using a Web Server Gateway Interface (WSGI) server (numerous possibilities are listed in the deployment docs with further instructions for each) that will function as your web/application server and call Flask as it serves requests.

Arthur Dent
  • 1,828
  • 13
  • 21
  • Thank you for your answer but I still don't understand how to do that. Could you please guide me about Web Server Gateway Interface (WSGI) – Harshit Satya May 11 '18 at 08:45
  • 5
    If you don't know about how to set up a WSGI or where to look for instructions, chances are that you don't need it. As long as you're not trying to run your Flask code in production (= publicly available website), you'll be fine with Flask's built-in webserver. Just make sure to set `threaded=True` as shown [here](https://stackoverflow.com/questions/14814201) if you have multiple clients accessing your service. – IonicSolutions May 11 '18 at 09:59
  • 2
    @HarshitSatya the documentation I linked to guides you through a whole bunch of various WSGI options. For development it isn't needed. Explaining beyond this would be out of scope for this question and likely too broad ("how do I deploy my Flask app?"), as entire tutorials are dedicated to this online. Personally, I use pythonanywhere because it makes it dead simple to deploy. – Arthur Dent May 11 '18 at 15:29
  • 1
    Thank you @ IonicSolutions and @Arthur Dent – Harshit Satya May 13 '18 at 02:28
  • 2
    This should be accepted as the answer. Arthur Dent both explained the issue, and linked the documentation for how to solve it. Thanks, Arthur. – Edward Ned Harvey Jun 21 '18 at 10:44
  • I rolled back edit because the new version linked to does not contain this exact quote (only part of it). – Arthur Dent Aug 13 '18 at 16:00
45

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

lashgar
  • 5,184
  • 3
  • 37
  • 45
19

As of Flask 1.x, the default environment is set to production.

To use the development environment, create a file called .flaskenv and save it in the top-level (root) of your project directory. Set the FLASK_ENV=development in the .flaskenv file. You can also save the FLASK_APP=myapp.py.

Example:

myproject/.flaskenv:

FLASK_APP=myapp.py
FLASK_ENV=development

Then you just execute this on the command line:

flask run

That should take care of the warning.

simhumileco
  • 31,877
  • 16
  • 137
  • 115
Christian Hur
  • 429
  • 3
  • 10
7

To remove the "Do not use the development server in a production environment." warning, run:

export FLASK_ENV=development

before flask run.

Franck Dernoncourt
  • 77,520
  • 72
  • 342
  • 501
3

I was typing flask run and then saw this message after that I solve this issue with these:

1- Add this text in your myproject/.flaskenv :
FLASK_APP=myapp.py
FLASK_ENV=development
also you should type "pip3 install python-dotenv" for using this file .flaskenv

2-in your project folder type in terminal your flask command which one you use :
flask-3 run

gok han
  • 41
  • 5
3

First, try to the following :

set FLASK_ENV=development 

then run your app.

Meloman
  • 3,558
  • 3
  • 41
  • 51
Parth
  • 31
  • 4
1

I have been using flask for quite some time now, and today, suddenly this warning turned up. I found this.

As mentioned here, as of flask version 1.0 the environment in which a flask app runs is by default set to production. If you run your app in an older flask version, you won't be seeing this warning.

New in version 1.0.

Changelog

The environment in which the Flask app runs is set by the FLASK_ENV environment variable. If not set it defaults to production. The other recognized environment is development. Flask and extensions may choose to enable behaviors based on the environment.

Community
  • 1
  • 1
1

in configurations or config you can add this code : ENV = ""

same as if you try to add debug set to true like this DEBUG = True

for more detail you can check this http://flask.pocoo.org/docs/1.0/config/#ENV

Arian Saputra
  • 356
  • 6
  • 15
  • 1
    Thank you for your answer but at that time I already tried that. and DEBUG = True helps in so that we do not have to restart our session of flask each time we run our code – Harshit Satya Aug 02 '18 at 01:42
1

You can begin your main script like this :

import os

if __name__ == '__main__': 
    os.environ.setdefault('FLASK_ENV', 'development')
mathieu.letombe
  • 343
  • 1
  • 6
1

It means the programe is run on production mode even in developing environment.so to avoid that warning, you need to define this is development environment.for that,Type and run below command in project directory on terminal(linux).

export FLASK_ENV=development

if you are windows user then run,

set FLASK_ENV=development
Pasindu Perera
  • 489
  • 3
  • 8
1

To disable the message I use:

app.env = "development"

You have to put this in the Python-Script before you run the app with:

app.run(host="localhost")
FBattle206
  • 69
  • 6
0

If you encounter NoAppException and you see lazy loading the following seemed to fix the issue:

cd <project directory>
export FLASK_APP=.
export FLASK_ENV=development
export FLASK_DEBUG=1
user13476428
  • 71
  • 1
  • 1
  • 3
0

I was trying to run flask_socketio app and got this error.

The solution was just to install gevent: pip install gevent.

From the docs:

"The Flask development server based on Werkzeug can be used as well, with the caveat that this web server is intended only for development use, so it should only be used to simplify the development workflow and not for production. The extension automatically detects which asynchronous framework to use based on what is installed. Preference is given to eventlet, followed by gevent. For WebSocket support in gevent, uWSGI is preferred, followed by gevent-websocket. If neither eventlet nor gevent are installed, then the Flask development server is used."

Roy O'Bannon
  • 179
  • 1
  • 9