Skip to content

Debugging: Flask

Don Jayamanne edited this page Oct 24, 2016 · 5 revisions

Debugging Flask Applications is as simple as debugging any other Python Application.

Solution 1 (Flask 0.11.x):

Originally documented by KEATH MILLIGAN on 3 October 2016

If you are using Flask 0.11.x, chances are you use the new “flask” command to launch your app. You could add a run script just for debugging, but that isn’t necessary. Here’s how you can debug your app without modifying your code:

First, be sure you have your virtual environment configured in VSCode (you are using a virtual environment, right?). Select Preferences > Workspace Settings from the menu. Your .vscode/settings.json file should have a line something like this:

"python.pythonPath": "/Users/kmilligan/.virtualenvs/flask/bin/python"

In the .vscode directory, create a file named launch.json if it does not already exist. Change the “Flask” entry as follows:

Linux/OS X

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Flask (linux/osx)",
            "type": "python",
            "request": "launch",
            "stopOnEntry": false,
            "pythonPath": "${config.python.pythonPath}",
            "program": "${env.HOME}/.virtualenvs/flask/bin/flask",
            "env": {
                "FLASK_APP": "${workspaceRoot}/quickstart/app.py"
            },
            "args": [
                "run",
                "--no-debugger",
                "--no-reload"
            ],
            "debugOptions": [
                "WaitOnAbnormalExit",
                "WaitOnNormalExit",
                "RedirectOutput"
            ]
        }
    ]
}

Windows:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Flask",
            "type": "python",
            "request": "launch",
            "stopOnEntry": false,
            "pythonPath": "${config.python.pythonPath}",
            "program": "${workspaceRoot}/run.py",
            "env": {
                "FLASK_APP": "${workspaceRoot}/app.py"
            },
            "args": [
                "run",
                "--no-debugger",
                "--no-reload"
            ],
            "debugOptions": [
                "WaitOnAbnormalExit",
                "WaitOnNormalExit",
                "RedirectOutput"
            ]
        }        
    ]
}

Windows Note:

Also on Windows, you will need to add a “run.py” launcher script to the root of your project. This script will use the Flask CLI module to launch your app so it will behave consistently with “flask run”:

run.py:

import sys
import re
from flask.cli import main
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(main())
  • Unless you really need to debug app startup, set stopOnEntry to false so it won’t break in library code.
  • Change program to the path to your “flask” command in the virtual environment. You can get this from the command line (with the virtual environment active) by typing “which flask” (Mac OSX/Linux/Unix) or “where flask” (Windows).
  • Change the FLASK_APP environment variable to the name of your app’s bootstrap file.
  • Set --no-debugger to avoid any potential conflicts with the Werkzueg debugger.
  • Set --no-reload. The Python debugger doesn’t support module reloading.

Now set some breakpoints and start debugging!

Solution 2:

The following section uses the sample application outlined here (assumes you have an old-style “run.py” script in your project). The sample flask application can be downloaded form here.

  • Download and extract the above flask application
  • Open the directory in Visual Studio Code
  • Modify the run.py file as follows:
#!flask/bin/python
from app import app

#Do not add debug=True
#app.run(debug=True)

app.run()
  • Debug the Flask application using the standard Python debug configuration (see below):
        {
            "name": "Python",
            "type": "python",
            "request": "launch",
            "stopOnEntry": false,
            "pythonPath": "${config.python.pythonPath}",
            "program": "${file}",
            "debugOptions": [
                "WaitOnAbnormalExit",
                "WaitOnNormalExit",
                "RedirectOutput"
            ]
        },

Note:

  • Breakpoints added to the views work as expected
Clone this wiki locally