简体   繁体   中英

Error: when I run flask app in visual studio code

I write a minimum demo flask app in vscode:

from flask import Flask

app = Flask(__name__)


@app.route('/')
def index():
    return "Hello World!"

When I run it in vscode, it give me an error:

Error: Could not import "D".

The problem is I don't import any "D" packages, so I have no idea where this error come from and how to debug it.

I try to run this app in powershell, and it works as expected. So I think there may be some problem in my personal configurations of vscode. Below is my launch.json file of this project:

    {
        "name": "Python: Flask (0.11.x or later)",
        "type": "python",
        "request": "launch",
        "module": "flask",
        "env": {
            "FLASK_APP": "${workspaceFolder}/hello.py"
        },
        "args": [
            "run",
            "--no-debugger",
            "--no-reload"
        ]
    }

My user settings:

{
    "workbench.startupEditor": "newUntitledFile",
    "explorer.confirmDelete": false,
    "git.enableSmartCommit": true
}

My workspace settings:

{
    "python.pythonPath": "${workspaceRoot}/venv/Scripts/python.exe",
    "python.formatting.provider": "yapf",
}

Thanks in advance for Any suggestions on where the problem is or how to debug it.

I did two things two work around this:

  1. I renamed my app to app.py (so in your case, rename "hello.py" to "app.py").
  2. I set my launch config "FLASK_APP" entry to:

"FLASK_APP": " PATH_FROM_CWD_TO_APP_FOLDER \\\\app.py"

What is PATH_FROM_CWD_TO_APP_FOLDER? Suppose you are running your app in the folder \\foo , and app.py is in \\foo\\bar\\baz .* Then PATH_FROM_CWD_TO_APP_FOLDER is bar\\baz , and your "FLASK_APP" entry would be

"FLASK_APP": "bar\\\\baz\\\\app.py"

* How do you know which folder you're running your app from? Check the terminal and see what directory the commands to run flask are being run from. That's the directory your app is being run from.

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