Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask not activating debug mode

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

like image 934
Alejo Dev Avatar asked May 04 '20 03:05

Alejo Dev


People also ask

How do I enable debug mode in flask?

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.

How to run flask app in debug mode?

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)

Is there a way to debug flask_debug env ARG?

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.

What is flask debugger toolbar?

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.

How to enable debug mode in Python?

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.


Video Answer


3 Answers

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

enter image description here

like image 58
Faisal Abdul Rahiman Avatar answered Oct 18 '22 05:10

Faisal Abdul Rahiman


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.

like image 40
Satyam Avatar answered Oct 18 '22 03:10

Satyam


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 !!

like image 5
Salah Avatar answered Oct 18 '22 04:10

Salah