bongbang bongbang - 1 year ago 157
Python Question

Canonical way to run Flask app locally

The official Flask documentation uses

flask run
python -m flask run
, both of which require that
be set. Most other tutorials I've seen, however, simply use
, which doesn't require the extra step and which has worked well for me so far.

What are the advantages of
flask run
, if any? I want to make sure that the alternative doesn't lead to a bug that I can't figure out later on.

Answer Source

Unless you have a reason not to (and you probably don't), use flask run to run the development server. It is what is supported going forward. Paraphrasing from the docs:

from Flask 0.11 onward the flask command is recommended. The reason for this is that due to how the dev server's reload mechanism works there are some bizarre side-effects when using (like executing certain code twice, sometimes crashing without message or dying when a syntax or import error happens).

To solve these problems, the flask command separates the app from the code that imports the app and runs the server. The method still exists because none of those issues were critical, only confusing. It may be fully deprecated in the future.

Besides the run command, it also provides the ability to add other useful commands that can be run inside the app context, in place of separate extensions or scripts.

As always, the same warning still applies: do not run the development server in production.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download