简体   繁体   English

VS Code Debugger 导入错误,同时尝试调试 flask 应用程序

[英]VS Code Debugger import error whilst trying to debug flask app

I currently have a flask application in which the file structure looks like this:我目前有一个 flask 应用程序,其中文件结构如下所示:

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

And the folder I currently have open as a workspace in VS Code is the BGSCS-API-dev folder.我目前在 VS Code 中作为工作区打开的文件夹是 BGSCS-API-dev 文件夹。

My launch.json currently looks like this:我的 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
        }
    ]
}

And upon every debugging attempt, I get an error.在每次调试尝试时,我都会收到错误消息。

 * 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'.

I know that there is probably something I need to add to my launch.json file to path to the file properly, but the things I have tried haven't worked for me.我知道我可能需要将一些内容添加到我的 launch.json 文件中以正确路径到文件,但是我尝试过的东西对我没有用。 Thanks谢谢

Okay, so I've solved the initial error I was having, only to be faced with a new error pertaining to another import issue, this time actually inside the code, importing a file that imports fine when running the program normally.好的,所以我已经解决了最初的错误,只是遇到了与另一个导入问题有关的新错误,这一次实际上是在代码中,导入了一个在正常运行程序时可以正常导入的文件。

The fix I implemented to my code is as follows (within launch.json)我对我的代码实施的修复如下(在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
        }
    ]
}

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

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