Debugging Flask Applications is as simple as debugging any other Python Application.
Originally documented by KEATH MILLIGAN on 13 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:
{
"version": "0.2.0",
"configurations": [
{
"name": "Flask",
"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"
]
}
]
}
stopOnEntry
to false so it won’t break in library code.FLASK_APP
environment variable to the name of your app’s bootstrap file.--no-debugger
to avoid any potential conflicts with the Werkzueg debugger.--no-reload
. The Python debugger doesn’t support module reloading.Now set some breakpoints and start debugging!
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.
#!flask/bin/python
from app import app
#Do not add debug=True
#app.run(debug=True)
app.run()
{
"name": "Python",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "${config.python.pythonPath}",
"program": "${file}",
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput"
]
},
Note:
Debugging Flask Applications is as simple as debugging any other Python Application.
Originally documented by KEATH MILLIGAN on 13 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:
{
"version": "0.2.0",
"configurations": [
{
"name": "Flask",
"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"
]
}
]
}
stopOnEntry
to false so it won’t break in library code.FLASK_APP
environment variable to the name of your app’s bootstrap file.--no-debugger
to avoid any potential conflicts with the Werkzueg debugger.--no-reload
. The Python debugger doesn’t support module reloading.Now set some breakpoints and start debugging!
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.
#!flask/bin/python
from app import app
#Do not add debug=True
#app.run(debug=True)
app.run()
{
"name": "Python",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "${config.python.pythonPath}",
"program": "${file}",
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput"
]
},
Note: