简体   繁体   中英

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:

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.

My launch.json currently looks like this:

{
    // 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. 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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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