Not registered yet?
Register now! It is easy and done in 1 minute and gives you access to special discounts and much more!
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?
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:
Now issue the following command:
Use loggers and print statements in the Development Environment, you can go for sentry in case of production environments.
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.
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.
Please edit your answer to provide some narrative re how it answers the question. Thanks. (Also, you appear to have duplicated a line.)
This works perfectly, thanks :)
Quick tip - if you use a PyCharm, go to Edit Configurations => Configurations and enable FLASK_DEBUG checkbox, restart the Run.
You might want to add that this checkbox is only available in PyCharm Professional. Reference: jetbrains.com/help/pycharm/…
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
app = Flask(__name__)
app.debug = True
app.secret_key = 'development key'
toolbar = DebugToolbarExtension(app)
logging.warning("See this message in Flask Debug Toolbar!")
Start the application as follows:
FLASK_APP=main.py FLASK_DEBUG=1 flask run
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.:
For Windows CMD, use set instead of export:
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.
If you're using Visual Studio Code, replace
It appears when turning on the internal debugger disables the VS Code debugger.
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.
If you are running it locally and want to be able to step through the code:
python -m pdb script.py
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
python -m flask run --host=0.0.0.0
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.
You can use app.run(debug=True) for the Werkzeug Debugger edit as mentioned below, and I should have known.
Actually, when you run with debug=True you are actually using the Werkzeug debuger so it's not an either-or ;-)
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.
I have set app.run(debug=True), if i do print xyz where does it print to, thanks
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.