Home > Back-end >  Running Flask by "flask run" vs running from editor (Windows 10)
Running Flask by "flask run" vs running from editor (Windows 10)

Time:10-20

If I run my Flask app through the command "flask run", not only the arguments in app.run are ignored (making debugger false and choosing the default port 5000), but changes made to the script too, e.g. changing url_prefix to "/views". The cmd prompt won't even respond when I save the script after making alterations. This way, changes are only effective after stopping the script then running again.
This doesn't happen when the app script is ran through VSCode commands (Ctrl F5 or the play button): through those, the script is executed as written and changes are recognized right after saving a script.
Why is that so?

from flask import Flask
from views import views

app = Flask(__name__)

app.register_blueprint(views, url_prefix="/")

if __name__ == '__main__':
    app.run(debug=True, port=8000)
C:\Users\*******\Documents\flask_quick_website>flask run
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL C to quit)

CodePudding user response:

The flask executable works by importing the app object from your code. This is similar to how a WSGI server like gunicorn works (research what this is if you're unsure).

As for when you run the script with VSCode, or in-fact if you launch with the python executable by running python app.py or similar, then anything inside the if __name__ == '__main__': block is executed, in your case the app.run call.

Also note your app.run call is passed the debug=True argument. If you investigate the Flask source code, inside the run function, the use_reloader argument is set to the same value as debug so in this case the auto-reloader runs if debug is True.


So how to make the auto-reloader work with the flask command? Pass the --reload flag:

flask run --reload

You can also set the environment to development to achieve the same. On windows:

> set FLASK_ENV=development
> flask run

See Environment and Debug features for more on this.

  • Related