Reputation: 516
I'm trying to run a flask application in debug mode (or at least a mode where it will reload after changing the files).
I'm aware of export FLASK_ENV=development
, however I am working on a university online development environment, and I lose the environment variables every time the site reloads, which while not the end of the world, is slightly annoying, and I'd rather avoid having to keep typing it (lazy I know).
If I include the following, and run using python3 main.py
, debug mode is activated, however when using flask run
, debug remains off.
if __name__ == "__main__":
app.run(debug=True)
However, as I understand it, using the flask run
command is the preferred way to launch the app, not using python app.py
.
I've found ideas such as including the following, however none of these have activated debug mode, so I'm wondering whether it is even possible:
app.config['ENV'] = 'development'
app.config['DEBUG'] = True
app.config['TESTING'] = True
I've simplified my code to the following to see if it was an error in my original piece, but it doesn't seem to be:
from flask import Flask
app = Flask(__name__)
app.config['ENV'] = 'development'
app.config['DEBUG'] = True
app.config['TESTING'] = True
@app.route('/')
def home():
return '<h1>debugging!</h1>'
if __name__ == "__main__":
app.run(debug=True)
Upvotes: 0
Views: 3573
Reputation: 637
Well, when it's Flask 2.2 or later, you can just do
flask --debug run
* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
* Restarting with stat
* Debugger is active!
* Debugger PIN: 560-342-853
Upvotes: 2
Reputation: 516
In short, there does not seem to be a way of using flask run
how I want without assigning environment variables, however using a .flaskenv
file will allow environment variables to be loaded at run time.
The .flaskenv
file for example could include the following ENVs among others to be loaded:
FLASK_APP=main:app
FLASK_ENV=development
FLASK_DEBUG=1
Note - this does require python-dotenv
to be installed to use.
All credit to @cizario who answered here with some more detail:
https://stackoverflow.com/a/64623193/12368419
Upvotes: 1