Sheldon
Sheldon

Reputation: 4653

Better way to solve the "No module named X" error in VS code?

I am unfamiliar with VS Code.

While trying to run a task (Terminal --> Run Task... --> Dev All --> Continue without scanning the task output) associated with a given code workspace (module.code-workspace), I ran into the following error, even though uvicorn was installed onto my computer:

/usr/bin/python: No module named uvicorn

I fixed this issue by setting the PATH directly into the code workspace. For instance, I went from:

"command": "cd ../myapi/ python -m uvicorn myapi.main:app --reload --host 127.x.x.x --port xxxx"

to:

"command": "cd ../myapi/ && export PATH='/home/sheldon/anaconda3/bin:$PATH' && python -m uvicorn myapi.main:app --reload --host 127.x.x.x --port xxxx"

While this worked, it seems like a clunky fix.

Is it a good practice to specify the path in the code workspace?

My understanding is that the launch.json file is meant to help debug the code. I tried creating such a file instead of fiddling with the workspace but still came across the same error.

In any case, any input on how to set the path in VS code will be appreciated!

Upvotes: 0

Views: 1051

Answers (1)

Steven-MSFT
Steven-MSFT

Reputation: 8431

You can modify the tasks like this:

"tasks": [
    {
        "label": "Project Name",
        "type": "shell",
        "command": "appc ti project id -o text --no-banner",
        "options": {
            "env": {
                "PATH": "<mypath>:${env:PATH}"
            }
        }
    }
]

Upvotes: 1

Related Questions