How do I configure Visual Studio Code to debug a Flask (Python) web application?
For example, when I set the debugger in the view function, it should allow me to step through that function when we hit that route in the browser.
I have already installed the Python extension in Visual Studio Code.
Here is my configuration for Flask 0.12, Python 3.6, and Visual Studio Code 1.20
// File "launch.json"
{
"version": "0.2.0",
"configurations": [
{
"name": "Flask",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "${config:python.pythonPath}",
"program": "${workspaceRoot}/app.py",
"env": {
"FLASK_APP": "${workspaceRoot}/app.py"
},
"args": [
"run"
],
"envFile": "${workspaceFolder}/.env",
"debugOptions": [
"RedirectOutput"
]
}
]
}
# app.py file
app.run(port=5000)
# Don't use debug=True, because it disables the Visual Studio Code debugger
# app.run(port=5000, debug=True) - disables the Visual Studio Code debugger
I don't use Visual Studio Code for Python development. However, Flask has a really nice debugging option, that allows you to debug from a browser. This is not a solution for Visual Studio Code, but a workaround.
When you define your application, pass the debug = true
parameter to enable debugging mode. Then you can debug your code from the browser.
app = Flask(__name__)
app.config['DEBUG'] = True
More information can be found here.
I found the following solution working for me. I followed the official tutorial with some tweaks in the generated launch.json file.
Visual Studio Code official flask tutorial debugging section
My setup is:
My current launch.json file is:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "app.py",
"FLASK_ENV": "development",
"FLASK_DEBUG": "1" // make sure it is not "0"
},
"args": [
"run",
// "--no-debugger", Comment out this line
// "--no-reload" Comment out this line
],
"jinja": true
}
]
}
By default the generated launch.json file had the
lines, which prevented me from debugging.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With