215

How are you meant to debug errors in Flask? Print to the console? Flash messages to the page? Or is there a more powerful option available to figure out what's happening when something goes wrong?

davidism
  • 121,510
  • 29
  • 395
  • 339
Kimmy
  • 3,787
  • 5
  • 22
  • 20

13 Answers13

223

Running the app in debug mode will show an interactive traceback and console in the browser when there is an error. As of Flask 2.2, to run in debug mode, pass the --app and --debug options to the flask command.

$ flask --app example --debug run

Prior to Flask 2.2, this was controlled by the FLASK_ENV=development environment variable instead. You can still use FLASK_APP and FLASK_DEBUG=1 instead of the options above.

For Linux, Mac, Linux Subsystem for Windows, Git Bash on Windows, etc.:

$ export FLASK_APP=example
$ export FLASK_DEBUG=1
$ flask run

For Windows CMD, use set instead of export:

set FLASK_DEBUG=1

For PowerShell, use $env:

$env:FLASK_DEBUG = "1"

If you're using the app.run() method instead of the flask run command, pass debug=True to enable debug mode.

Tracebacks are also printed to the terminal running the server, regardless of development mode.

If you're using PyCharm, VS Code, etc., you can take advantage of its debugger to step through the code with breakpoints. The run configuration can point to a script calling app.run(debug=True, use_reloader=False), or point it at the venv/bin/flask script and use it as you would from the command line. You can leave the reloader disabled, but a reload will kill the debugging context and you will have to catch a breakpoint again.

You can also use pdb, pudb, or another terminal debugger by calling set_trace in the view where you want to start debugging.


Be sure not to use too-broad except blocks. Surrounding all your code with a catch-all try... except... will silence the error you want to debug. It's unnecessary in general, since Flask will already handle exceptions by showing the debugger or a 500 error and printing the traceback to the console.

davidism
  • 121,510
  • 29
  • 395
  • 339
56

You can use app.run(debug=True) for the Werkzeug Debugger edit as mentioned below, and I should have known.

bnlucas
  • 1,724
  • 1
  • 13
  • 18
  • I have set app.run(debug=True), if i do print xyz where does it print to, thanks – Kimmy Jun 28 '13 at 11:40
  • Using `print 'xyz'` will print to the console. If you want to debug in the browser, you will need to force an error on where you are wanting to debug. `raise Exception('xyz')`. This will trigger the debug to output in the browser window. – bnlucas Jun 28 '13 at 15:09
33

From the 1.1.x documentation, you can enable debug mode by exporting an environment variable to your shell prompt:

export FLASK_APP=/daemon/api/views.py  # path to app
export FLASK_DEBUG=1
python -m flask run --host=0.0.0.0
Édouard Lopez
  • 40,270
  • 28
  • 126
  • 178
26

One can also use the Flask Debug Toolbar extension to get more detailed information embedded in rendered pages.

from flask import Flask
from flask_debugtoolbar import DebugToolbarExtension
import logging

app = Flask(__name__)
app.debug = True
app.secret_key = 'development key'

toolbar = DebugToolbarExtension(app)

@app.route('/')
def index():
    logging.warning("See this message in Flask Debug Toolbar!")
    return "<html><body></body></html>"

Start the application as follows:

FLASK_APP=main.py FLASK_DEBUG=1 flask run
Shubham
  • 2,847
  • 4
  • 24
  • 37
turdus-merula
  • 8,546
  • 8
  • 38
  • 50
19

If you're using Visual Studio Code, replace

app.run(debug=True)

with

app.run()

It appears when turning on the internal debugger disables the VS Code debugger.

Eman4real
  • 538
  • 5
  • 12
14

If you want to debug your flask app then just go to the folder where flask app is. Don't forget to activate your virtual environment and paste the lines in the console change "mainfilename" to flask main file.

export FLASK_APP="mainfilename.py"
export FLASK_DEBUG=1
python -m flask run --host=0.0.0.0

After you enable your debugger for flask app almost every error will be printed on the console or on the browser window. If you want to figure out what's happening, you can use simple print statements or you can also use console.log() for javascript code.

Frak
  • 832
  • 1
  • 11
  • 32
omkar yadav
  • 177
  • 5
  • 15
12

To activate debug mode in flask you simply type set FLASK_DEBUG=1 on your CMD for windows, or export FLASK_DEBUG=1 on Linux terminal then restart your app and you are good to go!!

alexis
  • 48,685
  • 16
  • 101
  • 161
Zahid
  • 129
  • 1
  • 4
11

When running as python app.py instead of the flask command, you can pass debug=True to app.run.

if __name__ == "__main__":
    app.run(debug=True)
$ python app.py
davidism
  • 121,510
  • 29
  • 395
  • 339
Tushar
  • 880
  • 9
  • 17
9

Install python-dotenv in your virtual environment.

Create a .flaskenv in your project root. By project root, I mean the folder which has your app.py file

Inside this file write the following:

FLASK_APP=myapp 
FLASK_ENV=development

Now issue the following command:

flask run
MSS
  • 3,306
  • 1
  • 19
  • 50
6

with virtual env activate

export FLASK_DEBUG=true

you can configure

export FLASK_APP=app.py  # run.py
export FLASK_ENV = "development"

to start

flask run

the result

 * Environment: development
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: xxx-xxx-xxx

and if you change

export FLASK_DEBUG=false

 * Environment: development
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Ioan Beilic
  • 463
  • 7
  • 12
5

For Windows users:

Open Powershell and cd into your project directory.

Use these commandos in Powershell, all the other stuff won't work in Powershell.

$env:FLASK_APP = "app"  
$env:FLASK_ENV = "development"
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
sn98
  • 51
  • 1
  • 1
3

If you have PyCharm Professional, you can create a Flask server run configuration and enable the FLASK_DEBUG checkbox. Go to Run > Edit Configurations, select or create a Flask server configuration, and enable the FLASK_DEBUG checkbox. Click OK, then click the run button.

davidism
  • 121,510
  • 29
  • 395
  • 339
Sgryt
  • 290
  • 3
  • 13
0

You can install python-dotenv with pip install python-dotenv then create a .flask_env or a .env file The contents of the file can be:

FLASK_APP=myapp
FLASK_DEBUG=True
collinsmarra
  • 148
  • 1
  • 7