The following ways allows me to launch the Flask server.
Option 1:
set FLASK_APP = app.py
flask run
Option 2:
set FLASK_APP = app.py
python -m flask run
Option 3:
python app.py
What is the difference between using either of this?
The app. run() method supports several options, including all those you can provide to the flask run command, and a few more: host – the hostname to listen on. port – the port of the web server. debug – if given, enable or disable debug mode.
You can use Flask's development server to inspect any changes that you make to the code of your Python app. The server listens to changes you make in the code and will automatically reload to display them. If your app doesn't render as you expect it to on the development server, then it won't work in production either.
app.py is the generic entry point for Abseil Python applications. This is a key difference from how python applications are typically run, where you identify a specific file as the entry point, e.g., $ python my_app.py .
Although Flask has a built-in web server, as we all know, it's not suitable for production and needs to be put behind a real web server able to communicate with Flask through a WSGI protocol.
$ 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 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(port=5500)
, but it's going to be "messy" modifying the code based on environment-related configs that are unrelated to the code.
That is why we have the second way, 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, we have the third way:
$ 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.8 -m flask --version
Python 3.8.10
Flask 1.1.2
Werkzeug 1.0.1
$ python3.8 -m flask run
$ python3.7 -m flask --version
Python 3.7.4
Flask 1.1.1
Werkzeug 0.16.0
$ python3.7 -m flask run
$ python -m flask --version
Python 2.7.16
Flask 1.0.3
Werkzeug 0.14.1
$ python -m flask run
flask run
This one looks for an executable (called flask
) on your PATH, the first one executes with a parameter run
, which will make the flask helper run the application by calling FLASK_APP.
python -m flask run
This one looks for an executable called python
on your PATH, the first one executes receiving -m
as argument, which is supposed to run a module (flask) and then pass the parameter run to it.
The key difference here, is that as it executes the first found executable on PATH, you can run a totally different Flask from the first one. You can also run a different python version's Flask.
python app.py
This calls the first python
executable on PATH, and passes app.py
as argument. It will make python run the app.py script, which may or may not have app.run() (This is what starts Flask).
If you don't have anything inside app.py, it won't call Flask's server.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With