【问题标题】:VS Code Debugger import error whilst trying to debug flask appVS Code Debugger 在尝试调试烧瓶应用程序时导入错误
【发布时间】:2022-01-02 04:35:58
【问题描述】:

我目前有一个烧瓶应用程序,其中文件结构如下所示:

C:\Users\kmelton\Python\Flask\BGSCS-API-dev\InterjectApi\server.py

我目前在 VS Code 中作为工作区打开的文件夹是 BGSCS-API-dev 文件夹。

我的 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": "server.py",
                "FLASK_ENV": "development"
            },
            "args": [
                "run",
                "--no-debugger"
            ],
            "jinja": true
        }
    ]
}

在每次调试尝试时,我都会收到一个错误。

 * Serving Flask app 'server.py' (lazy loading)
 * Environment: development
 * Debug mode: on
Usage: python -m flask run [OPTIONS]
Try 'python -m flask run --help' for help.

Error: Could not import 'server'.

我知道我可能需要将一些内容添加到我的 launch.json 文件中以正确地指向文件的路径,但是我尝试过的东西对我不起作用。谢谢

【问题讨论】:

    标签: python debugging flask visual-studio-code vscode-debugger


    【解决方案1】:

    好的,所以我已经解决了最初的错误,只是遇到了与另一个导入问题有关的新错误,这一次实际上是在代码中,导入了一个在正常运行程序时可以正常导入的文件。

    我对我的代码实施的修复如下(在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",
                # Added new line cwd to specify initial working directory
                "cwd":"${workspaceRoot}",
                "env": {
                    # Added ${workspaceRoot}/InterjectApi/"file name trying to run"}
                    "FLASK_APP": "${workspaceRoot}/InterjectApi/server.py",
                    "FLASK_ENV": "development"
                },
                "args": [
                    "run",
                    "--no-debugger"
                ],
                "jinja": true
            }
        ]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-23
      • 1970-01-01
      • 1970-01-01
      • 2021-06-10
      • 1970-01-01
      • 1970-01-01
      • 2021-12-12
      • 1970-01-01
      相关资源
      最近更新 更多