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
Another way on how we can enable the debug mode is by passing debug=True in the python code itself. Now when a debugger is running, when an error is encountered, it starts from the line of code tries to trace it back to the main function.
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)
And the same, only command line would read the FLASK_DEBUG env arg, and if you using manage.py to run your app, it also not affect the debug mode. Yes, but the issue is that there seems to be no way to enter debug mode when using a custom management script.
Flask also provides a debugger toolbar to debug our web application on a larger scale. Now, why a toolbar? Well, all developers, at some point in life, come across errors. Searching and eliminating them is not an easy task.
Another way on how we can enable the debug mode is by passing debug=True in the python code itself. Now when a debugger is running, when an error is encountered, it starts from the line of code tries to trace it back to the main function.
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:
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.
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 !!
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