$ python app.py
This is the simplest, standard way of calling the Python interpreter to run any Python script. It is not specific to Flask. The app.py may or may not have a if __name__ == "__main__"
block (see What does if __name__ == "__main__": do?), but if you are going to do this for Flask, it is required to have __main__
method that calls app.run()
. From the Flask docs:
The alternative way to start the application is through the
Flask.run() method. This will immediately launch a local server
exactly the same way the flask script does.
Example:
if __name__ == '__main__':
app.run()
The same docs also state why even though this works, it is not recommended:
This works well for the common case but it does not work well for
development which is why from Flask 0.11 onwards the flask
method is
recommended. The reason for this is that due to how the reload
mechanism works there are some bizarre side-effects (like executing
certain code twice, sometimes crashing without message or dying when a
syntax or import error happens).
This way is also problematic if you need to modify run configurations (ex. port) depending on the host environment. For example, you need to use port 5500 instead of the default 5000 when running on a certain machine. You can of course do this with os.environ
and app.run(host=5500)
, but it's going to be "messy" modifying the code based on environment-related configs that are unrelated to the code.
Enter the flask command line tool.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
$ set FLASK_APP=app.py
$ flask run --port=5500
You can now maintain your code to be independent of any external environment configurations. Aside from that, the flask
CLI tool has a lot of other options for configuration and debugging, such as enabling/disabling DEBUG mode, listing routes (flask routes
), and getting env vars from .env files.
Notice also that your app does not have to explicitly call app.run
and __name__
now isn't going to be __main__
. This is helpful for cases where your app is just part of a bigger package and/or it needs to be run from some other directory. See the Larger Applications section of the Flask docs.
Finally,
$ python -m flask run
This is another standard way of running Python scripts. It is also not specific to Flask. From the docs:
When called with -m module-name
, the given module is located on the
Python module path and executed as a script.
This means flask
will be searched from the invoked python
module search path. This is particularly useful when your environment has multiple versions of Python and you want to make sure you are using the correct Python version and env with Flask. It can also be useful when you have multiple Flask installations for multiple projects. It explicitly sets which Python interpreter to use to call the flask
CLI tool.
$ python3.7 -m flask --version
Python 3.7.4
Flask 1.1.1
Werkzeug 0.16.0
$ python -m flask --version
Python 2.7.16
Flask 1.0.3
Werkzeug 0.14.1