[Python] VSCode and FastAPI debugging - doesn't break within routes

Discussão em 'Python' iniciado por Stack, Março 28, 2025.

  1. Stack

    Stack Membro Participativo

    Just getting started with FastAPI, but running into issues with trying to get it to recognize breakpoints in the VSCode debugger. The strange thing is that it does successfully break on lines that are not contained within routes

    Pulling directly from the tutorial: https://fastapi.tiangolo.com/tutorial/debugging/

    import uvicorn
    from fastapi import FastAPI

    app = FastAPI() # breakpoint here works on launching file
    print('here') # breakpoint here works on launching file


    @app.get("/")
    def root():
    a = "a" # breakpoint here does NOT work
    b = "b" + a
    return {"hello world": b} # returns data successfully


    if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=5002)


    As commented above, the lines outside the routes work fine, and when I go to the address I get the data successfully, but the breakpoint within the route does not fire. Not sure what I'm missing here. Have tried various solutions from Debug FastAPI application in VSCode

    I'm not sure if this makes a difference, but this is also running through the Remote-SSH extension on VSCode (my code is on a gcloud VM). Maybe this is contributing but again, the other breakpoints fire fine.

    My 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: Current File",
    "type": "python",
    "request": "launch",
    "program": "${file}",
    "console": "integratedTerminal"
    }
    ]
    }


    EDIT: Looks like it does work if I replace "def" with "async def", though I'm trying to get this to work with a SQLite DB and per this page: https://fastapi.tiangolo.com/tutorial/sql-databases/ it recommends using "def" by default (with options to configure to use async def). I'll need to look into the distinction some more.

    Continue reading...

Compartilhe esta Página