简体   繁体   English

如何在 VS Code 中调试 Flask 应用程序

[英]How do I debug Flask App in VS Code

I've been trying to get the debugger working in VS Code so that I can debug my Flask App.我一直在尝试让调试器在 VS Code 中工作,以便我可以调试我的 Flask 应用程序。 I have tries so many options in the launch.json that I feel there isn't any left.我在launch.json中尝试了很多选项,我觉得没有剩下的了。

the following examples did not work: https://github.com/DonJayamanne/pythonVSCode/wiki/Debugging:-Flask以下示例不起作用: https : //github.com/DonJayamanne/pythonVSCode/wiki/Debugging : -Flask

Debug Flask(Python) web application in Visual studio code 在 Visual Studio 代码中调试 Flask(Python) Web 应用程序

Below are my launch.json and setting.json .下面是我的launch.jsonsetting.json I have two configurations in the launch file as I was trying multiple variations.我在尝试多个变体时在启动文件中有两个配置。

launch.json启动文件

"version": "0.2.0",
    "configurations": [
    {
        "name": "Flask",
        "type": "python",
        "request": "launch",
        "stopOnEntry": false,
        "pythonPath": "${config:python.pythonPath}",
        //"module": "flask.cli",
        "program": "${workspaceRoot}/startup.py",
        "cwd": "${workspaceRoot}",
        "env": {
          "FLASK_APP": "${workspaceRoot}/apt-flask.py",
        },
        "args": [
          "run",
          "--no-debugger",
          "--no-reload"
        ],
        "envFile": "${workspaceRoot}/.env",
        "debugOptions": [
          "WaitOnAbnormalExit",
          "WaitOnNormalExit",
          "RedirectOutput"
        ]
    },
    {
        "name": "Python: APT FLask",
        "type": "python",
        "request": "launch",
        "stopOnEntry": true,
        "pythonPath": "${workspaceFolder}/venv/Scripts/python.exe",
        //"program": "${workspaceFolder}/venv/Scripts/flask.exe",
        "module": "flask.cli",
        "cwd": "${workspaceFolder}",
        "env": {
            "FLASK_APP": "${workspaceFolder}/apt-flask.py",
            "DEBUG": 1,
            "LC_ALL": "en_US.utf-8",
            "LANG": "en_US.utf-8"
        },
        "args": [
            "run",
            "--no-debugger",
            "--no-reload"
        ],
        "envFile": "${workspaceFolder}/.env",
        "debugOptions": [
            "WaitOnAbnormalExit",
            "WaitOnNormalExit",
            "RedirectOutput"
        ]
    }
]

settings.json设置.json

{
    "python.pythonPath": "${workspaceRoot}/venv/Scripts/python.exe"
}

As far as errors go, I get no errors in the console, only the error within the editor that tells me that the "Debug adapter process has terminated unexpectedly".就错误而言,控制台中没有错误,只有编辑器中的错误告诉我“调试适配器进程意外终止”。

I'm not sure what else to try.我不知道还有什么可以尝试的。 I currently use Pycharm but was looking for an editor that is more lightweight and as I use VS Code for other things it makes sense to change, so would be nice to finally get this working.我目前使用 Pycharm,但正在寻找一个更轻量级的编辑器,因为我将 VS Code 用于其他事情,所以改变是有意义的,所以最终让它工作会很好。

Any help would be brilliant.任何帮助都会很棒。

As of November 2019 I the found the following useful:截至 2019 年 11 月,我发现以下内容很有用:

New Way (basic & flakey) - "Old Way" Below is Better新方法(基本和 flakey)-下面的“旧方法”更好

Assuming a simple app.py such as:假设一个简单的 app.py 如:

import flask
app = flask.Flask(__name__)
@app.route('/')
def index():
    return "Hello world!"

And .vscode/launch.json added to your project by adding Python Flask Debug Configuration from the Debug Explorer drop down.通过从调试资源管理器下拉菜单中添加 Python Flask 调试配置,将 .vscode/launch.json 添加到您的项目中。

{
    // 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"
            },
            "args": [
                "run",
                "--no-debugger",
                "--no-reload"
            ],
            "jinja": true
        }
    ]
}

The Flask app is effectively run the "new way" from the VS Code debugger [F5]. Flask 应用程序有效地从 VS Code 调试器 [F5] 中以“新方式”运行。

python -m flask run

Old Way (better)旧方式(更好)

Miguel suggests running apps the old way, with additional flags, is better in the VS Code debugger. Miguel 建议在 VS Code 调试器中以旧方式运行应用程序并带有附加标志更好。

Add the following to app.py (from above):将以下内容添加到 app.py(从上面):

if __name__ == '__main__':
    app.run(use_debugger=False, use_reloader=False, passthrough_errors=True)

Modify .vscode/launch.json to look as follows:修改 .vscode/launch.json 如下:

{
    // 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": "app",
            "env": {
                "FLASK_APP": "app.py",
                "FLASK_ENV": "development",
                "FLASK_DEBUG": "1"
            },
            "args": [
                // "run",
                // "--no-debugger",
                // "--no-reload"
            ],
            "jinja": true
        }
    ]
}

So the Flask app is effectively run the "old way" from the VS Code debugger [F5].因此 Flask 应用程序有效地从 VS Code 调试器 [F5] 运行“旧方式”。

python app.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM