Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug a Flask (Python) web application in Visual Studio Code

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.

like image 528
Murtuza Z Avatar asked Aug 20 '16 16:08

Murtuza Z


3 Answers

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
like image 142
Vlad Bezden Avatar answered Oct 17 '22 16:10

Vlad Bezden


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.

like image 3
Bedi Egilmez Avatar answered Oct 17 '22 16:10

Bedi Egilmez


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:

  • Python 3.8.3
  • Flask 1.1.2
  • Visual Studio Code 1.46.0

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

  • "FLASK_DEBUG": "0"
  • "--no-debugger"
  • "--no-reload"

lines, which prevented me from debugging.

like image 3
Lac Avatar answered Oct 17 '22 16:10

Lac