[Python] Debug FastAPI application in VSCode

Discussão em 'Python' iniciado por Stack, Setembro 5, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to debug an application (a web api) that use FastAPI (uvicorn) I'm also using poetry and set the projev virtual environment in vscode.

    I read this tutorial to setup uvicorn and this one to setup vscode but I think I'm doing something wrong in set it up.

    I tried to setup the launch.json both as python: module and python: current file

    The problem seems that it doesn't recognize the project structure cause when I run the debug it stopped in an import statement with this error:

    Exception has occurred: ImportError
    attempted relative import with no known parent package


    This is my current launch.json configuration:

    "configurations": [
    {
    "name": "Python: local debug",
    "type": "python",
    "request": "launch",
    "program": "${workspaceFolder}/src/topic_service/service/__init__.py",
    "args" : ["--port", "8000"]
    },
    ]


    I also tried to add a .env file setting PYTHONPATH:

    PYTHONPATH=.:${PYTHONPATH}


    Locally i run the application as follow:

    poetry run uvicorn src.main:app --port 8080 --reload


    Does anyone know how to correctly setup vscode to debug an uvicorn application?

    UPDATE: I also tried what this article says. the debugger seems to start but nothing happen (no breakpoint is triggered)

    Continue reading...

Compartilhe esta Página