18

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

Alejo Dev
  • 2,290
  • 4
  • 29
  • 45

7 Answers7

13

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

  • 1
    This worked for me as well. I mean, the command. I was running the app via `flask run` and it ignore every setting for me. Any clue what's the difference in these 2 commands? – Xonshiz Oct 18 '21 at 04:38
  • 2
    @Xonshiz `flask run` runs `app.py` as a module, so `__name__ != "__main_"`. This command runs the file directly so `__name__ == "__main__"`. – Xbox One Feb 23 '23 at 20:38
  • Oh, I see. I had forgotten about this comment lol. Thanks for update :D – Xonshiz Feb 25 '23 at 09:15
7

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

Salah
  • 513
  • 7
  • 18
  • Thank you. This worked for me. The issue I was having is that the Powershell command was different than the command prompt command. I was trying the command prompt command on powershell – ThePrince Oct 01 '21 at 05:48
3

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.

Satyam
  • 29
  • 5
1

There are two way to run Flask App.

  • Environment Variable
  • Programmatically

    Environment Variable you need to 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
Tejas Mankar
  • 108
  • 1
  • 7
1

Same issue I also faced. I was running the application via flask run. and my debug always remains off. Then I try to run it via python app.py and then debug is on.

So, just try to run with "python app.py"

0
  • Make sure that you're running the right program, sometimes it happens that we're making changes in some other program and running something else.
  • Also ensure that Setting app.debug = True is altogether a different thing from setting FLASK_DEBUG mode to on.

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)
Shivam Sahil
  • 4,055
  • 3
  • 31
  • 62
0

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