在 Visual Studio Code 中调试 Flask (Python) Web 应用程序 [英] Debug a Flask (Python) web application in Visual Studio Code

查看:41
本文介绍了在 Visual Studio Code 中调试 Flask (Python) Web 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何配置 Visual Studio Code 以调试 Flask (Python) Web 应用程序?

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.

我已经在 Visual Studio Code 中安装了 Python 扩展.

I have already installed the Python extension in Visual Studio Code.

推荐答案

这是我对 Flask 0.12、Python 3.6 和 Visual Studio Code 1.20 的配置

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

这篇关于在 Visual Studio Code 中调试 Flask (Python) Web 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆