如何在VS Code中调试Flask App [英] How do I debug Flask App in VS Code

查看:89
本文介绍了如何在VS Code中调试Flask App的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图使调试器在VS Code中运行,以便可以调试Flask App.我在 launch.json 中尝试了太多选项,以至于我感觉没有剩余了.

I've been trying to get the debugger working in VS Code so that I can debug my Flask App. I have tries so many options in the launch.json that I feel there isn't any left.

以下示例不起作用: https://github.com/DonJayamanne/pythonVSCode/wiki/Debugging:-Flask

在Visual Studio代码中调试Flask(Python)Web应用程序

下面是我的 launch.json setting.json .当我尝试多种变体时,启动文件中有两种配置.

Below are my launch.json and setting.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

{
    "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".

我不确定还有什么尝试.我目前使用Pycharm,但我正在寻找一种更轻便的编辑器,并且由于我将VS Code用于其他用途,因此有必要进行更改,因此最终使它正常工作将是一件很不错的事情.

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.

任何帮助都会很棒.

推荐答案

截至2019年11月,我发现以下有用:

As of November 2019 I the found the following useful:

假设有一个简单的app.py,例如:

Assuming a simple app.py such as:

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

然后通过从调试资源管理器"下拉菜单中添加"Python Flask调试配置",将.vscode/launch.json添加到您的项目中.

And .vscode/launch.json added to your project by adding Python Flask Debug Configuration from the Debug Explorer drop down.

{
    // 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
        }
    ]
}

Flask应用程序已从VS Code调试器[F5]有效地以新方式"运行.

The Flask app is effectively run the "new way" from the VS Code debugger [F5].

python -m flask run

老路(更好)

Miguel建议在VS Code调试器中使用带有其他标志的旧方法运行应用程序更好.

Old Way (better)

Miguel suggests running apps the old way, with additional flags, is better in the VS Code debugger.

将以下内容添加到app.py中(从上方):

Add the following to app.py (from above):

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

修改.vscode/launch.json如下所示:

Modify .vscode/launch.json to look as follows:

{
    // 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
        }
    ]
}

因此,Flask应用程序实际上是通过VS Code调试器[F5]以旧方式"运行的.

So the Flask app is effectively run the "old way" from the VS Code debugger [F5].

python app.py

这篇关于如何在VS Code中调试Flask App的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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