4

I'm using Remote-Containers to debug an FastApi app. The container has all the dependecies installed. When I try to debug using the vscode debugger I got the error No module named uvicorn. But if I run uvicorn api.main:app it works.

/usr/bin/env /usr/bin/python3 /Users/juracylopes/.vscode/extensions/ms-python.python-2022.10.1/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher 59746 -- -m uvicorn api.main:app /Library/Developer/CommandLineTools/usr/bin/python3: No module named uvicorn

My launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: FastAPI",
            "type": "python",
            "request": "launch",
            "module": "uvicorn",
            "args": [
                "api.main:app"
            ],
            "jinja": true,
            "justMyCode": true
        }
    ]
} ```
1
  • 1
    If uvicorn is not installed as a module, the command line application should not be installed as well. So if you have the latter, the former IS installed in the same environment. The error you're getting is from a different Python than the one you can successfully run uvicorn, and therefore, the whole environment is different. Commented Jul 27, 2022 at 21:12

1 Answer 1

0

Perhaps you've installed uvicorn in a virtual environment in your workspace? In that case, the Python that VSCode is running for your debugging session might not be referencing your venv.

I'm using python -m venv .venv to create my virtual environment, so my venv Python is at ${workspaceFolder}/.venv/bin/python.

Consider adding this to your project .vscode/settings.json:

{
    "python.pythonPath": "${workspaceFolder}/.venv/bin/python"
}

And then reloading your session to enable remote debugging via a module with your session referencing the virtual environment.

Reference:

How to add virtual environment to VSCode launch JSON

Not the answer you're looking for? Browse other questions tagged or ask your own question.