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

后端 未结 8 1911
失恋的感觉
失恋的感觉 2020-12-11 04:28

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

8条回答
  •  一生所求
    2020-12-11 05:13

    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
    

提交回复
热议问题