I am getting started with flask, I am trying to follow some tutorials, but I have been unable to run Flask app in debug mode.
I tried the simplest code I found:
from flask import Flask
app = Flask(__name__)
app.debug = True
# I have also tried with a configuration
# app.config.from_object('config')
# file with constant
# DEBUG = True
@app.route('/')
def hello_world():
return 'Hello World!'
Then I run
export FLASK_APP=hello_world.py
flask run
But I allways get this output
* Serving Flask app "hello_world.py"
* 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)
When I run print(app.debug) I get False
This is the output of pip freeze:
click==7.1.2
Flask==1.1.2
itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
Werkzeug==1.0.1
And I have python 3.8.2
if you are using windows just type the following in your terminal :
in PowerShell:
$env:FLASK_ENV = "development"
flask run
in command prompt :
C:\path\to\app>set FLASK_ENV=development
flask run
and for mac you probably need to run this :
$ export FLASK_ENV=development
$ flask run
enjoy !!
Try This:
When you run,
export FLASK_APP=hello_world.py
Run this command after the above,
export FLASK_DEBUG=1
Finally,
flask run
I hope this would start the debug mode.
I have tried the below steps and it worked for me.
Have added these lines of codes to my app.py file.
if __name__ == "__main__":
app.run(debug=True)
Then I ran the program from the Terminal using the command line
python3 app.py
and it worked.
Please find the below screenshot
Try this, this works at my end.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == "__main__":
app.run(debug=True)
There are two way to run Flask App.
exporting the FLASK_APP file using command for window set FLASK_APP = *(file_name)*.set FLASK_APP = app.py`
And for mac
export FLASK_ENV=development
You can also control debug mode separately from the environment by exporting FLASK_DEBUG=1. (set command for mac and export for window)
set FLASK_APP = app.py
set FLASK_DEBUG=1
But sometimes debugger does not work on production servers. It is a major security risk to be used on production machines.
Programmatically you have to include main section on your Flask app.py main file. It will run your app in debug mode easily but do not use flask run command use only python (file_name) command.
from flask import Flask
app = Flask(__name__)
app.debug = True
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run(debug=True)
python app.py
Apparently, because debug mode is already off, stopping and rerunning it didn't work. add the line below to app.py and save.
if __name__ == "__main__":
app.run(debug=True)
restart the editor. then run
set FLASK_DEBUG=1
then run your project
python3 app.py