193

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."

davidism
  • 121,510
  • 29
  • 395
  • 339
Anatoly
  • 3,266
  • 4
  • 17
  • 28

6 Answers6

273

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.

Yuchen
  • 30,852
  • 26
  • 164
  • 234
  • 2
    Is there any difference between `serve(app, host="0.0.0.0", port=8080)` and `waitress-serve --call flaskr:create_app`. Which one do you prefer? – Michael Dec 30 '20 at 18:25
  • 3
    There is any fundamental differences. `waitress-serve` is just a convenient command line runner. I personally would prefer the former since it is more documented. – Yuchen Dec 30 '20 at 20:36
  • 4
    Weren't you going to write: "There **isn't** any fundamental differences"? – Michael Dec 31 '20 at 15:11
  • 8
    Oh, I missed the most important word. lol. Yea, there aren't any fundamental differences. – Yuchen Jan 01 '21 at 15:55
  • 1
    What exactly is "flaskr:create app"? If I have created a simple "test.py" in which there is one @api.route method, what should I pass as the command-line argument? Because obviously typing the same as your example did not work. – Damn Vegetables Jul 03 '21 at 19:51
  • @DamnVegetables Thanks for the question. `flaskr` is the name of the file and `create_app` is the function to create the application. I will update my answer to clarify that. – Yuchen Jul 03 '21 at 21:06
  • I don't know if it's only me but I had to create a `def create_app():` surrounding all my code with a `return app` at the end, and instead of your `if create_app():` I did `if __name__ == '__main__': app = create_app() app.run()` Maybe I have missed something from your example but anyway it works for me like that. I wanted to share if someone like me didn't understand. With your example I get `SyntaxError: 'return' outside function` – ZazOufUmI Aug 14 '21 at 07:44
  • 1
    @ZazOufUmI thanks a lot for pointing that out. It was a mistake from my last edit. I corrected it. It should be `def` instead `if`. – Yuchen Aug 14 '21 at 15:34
  • This answer doesn't answer the question on "why it happens", only points how to avoid the warning message... – Leonardo Papais Jan 03 '22 at 14:35
  • @LeonardoPapais It is due to this line https://github.com/pallets/flask/blob/6b0c8cdac1fb9bbec88de3a514907c19e372c72a/src/flask/cli.py#L649. We are not supposed to run the flask app directly for prod env. That's why it warms us. – Yuchen Jan 03 '22 at 17:26
  • `waitress` doesn't support `ssl` natively, you need to use a reverse proxy. – Pedro Lobito Apr 18 '22 at 01:43
112

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.

davidism
  • 121,510
  • 29
  • 395
  • 339
6

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

Abolfazl Rastgou
  • 655
  • 10
  • 13
3

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

VIX
  • 605
  • 4
  • 15
3

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
Omar Magdy
  • 2,331
  • 14
  • 13
  • 2
    FLASK_ENV is going to be deprecated. FLASK_DEBUG=0 turns OFF debug mode, which may not be a good idea during development/testing. – Kinjal Dixit Aug 29 '22 at 06:46
0

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.