By Kimmy


2013-06-26 00:51:42 8 Comments

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?

9 comments

@thisisayush 2019-01-05 10:45:43

Use loggers and print statements in the Development Environment, you can go for sentry in case of production environments.

@omkar yadav 2018-09-30 17:09:35

If you want to debug your flak app 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 happing you can use simple print statements or you can also use console.log() for javascript code.

@MandyShaw 2018-09-30 17:37:24

Please edit your answer to provide some narrative re how it answers the question. Thanks. (Also, you appear to have duplicated a line.)

@omkar yadav 2018-12-01 13:46:51

@MandyShaw Done!

@Harvest 1018 2019-01-02 15:36:17

This works perfectly, thanks :)

@Sgryt87 2018-08-26 22:34:12

Quick tip - if you use a PyCharm, go to Edit Configurations => Configurations and enable FLASK_DEBUG checkbox, restart the Run.

@cyroxx 2018-09-09 15:51:33

You might want to add that this checkbox is only available in PyCharm Professional. Reference: jetbrains.com/help/pycharm/…

@turdus-merula 2017-01-17 20:59:12

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

@davidism 2013-06-26 14:15:59

Running the app in development mode will show an interactive traceback and console in the browser when there is an error. To run in development mode, set the FLASK_ENV=development environment variable then use the flask run command (remember to point FLASK_APP to your app as well).

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

export FLASK_APP=myapp
export FLASK_ENV=development
flask run

For Windows CMD, use set instead of export:

set FLASK_ENV=development

For PowerShell, use $env:

$env:FLASK_ENV = "development"

Prior to Flask 1.0, this was controlled by the FLASK_DEBUG=1 environment variable instead.

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.

@Eman4real 2018-05-03 03:07:14

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.

@Brandon Dube 2018-05-31 18:57:40

Can you share an example of a working setup? I have FLASK_APP defined in my env already and the default config would not work. I tried this one -- pastebin.com/v8hBQ2vv and a number of similar permutations, but to no avail.

@rstackhouse 2016-12-08 17:56:49

If you are running it locally and want to be able to step through the code:

python -m pdb script.py

@Édouard Lopez 2016-08-04 21:04:52

From the 0.11 documentation, you can enable debug mode by exporting an environment variable:

export FLASK_APP=/daemon/api/views.py  # path to app
export FLASK_DEBUG=1
python -m flask run --host=0.0.0.0

@Michael Scheper 2017-04-12 19:47:38

This answer would be more helpful if it explained what debug mode actually is. Does it do more than allow debugging in the browser? Unfortunately, since I'm working on a REST API, that doesn't really help me much.

@bnlucas 2013-06-26 00:58:45

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

@Sean Vieira 2013-06-26 01:02:49

Actually, when you run with debug=True you are actually using the Werkzeug debuger so it's not an either-or ;-)

@bnlucas 2013-06-26 01:05:42

Ha, you're right. I guess I should have looked at Flask's setup.py for requirements. I use a modified copy of it for working under GAE where you have to initialize Werkzeug manually.

@Kimmy 2013-06-28 11:40:46

I have set app.run(debug=True), if i do print xyz where does it print to, thanks

@bnlucas 2013-06-28 15:09:28

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.

Related Questions

Sponsored Content

26 Answered Questions

[SOLVED] How do I list all files of a directory?

  • 2010-07-08 19:31:22
  • duhhunjonn
  • 3046087 View
  • 3339 Score
  • 26 Answer
  • Tags:   python directory

42 Answered Questions

[SOLVED] How do I sort a dictionary by value?

42 Answered Questions

[SOLVED] How do I check whether a file exists without exceptions?

43 Answered Questions

[SOLVED] How to merge two dictionaries in a single expression?

35 Answered Questions

[SOLVED] How do I debug Node.js applications?

32 Answered Questions

[SOLVED] Reference - What does this error mean in PHP?

24 Answered Questions

29 Answered Questions

[SOLVED] How to get useful error messages in PHP?

18 Answered Questions

[SOLVED] JavaScript: How do I print a message to the error console?

  • 2008-10-02 20:23:55
  • Mark Harrison
  • 827444 View
  • 428 Score
  • 18 Answer
  • Tags:   javascript debugging

27 Answered Questions

[SOLVED] How to debug in Django, the good way?

Sponsored Content