debuggingmodulefastapi

FastAPI won't recognize module changes


I keep my SQL hooks in a separate module. I added a function, and for the life of me I cannot get it to be registered in the fastAPI. I can alter the main file of the fast API, but it just keeps giving me an attribute not found error. I also cannot change anything about the SQL hooks module either. When I change a hook name, fastAPI doesn't give me an error or anything. It just keeps sitting there smiling like an idiot.

I tried using task kill to remove any uvicorn sessions, deleted the pycache, reseting the API, and no change. I was expecting to be able to do the thing I've done a billion times before, and add a new function to the module


Solution

  • 1. Always run Uvicorn from project root:

    python -m uvicorn main:app --reload --app-dir . --reload-dir .
    

    2. Add --reload-dir for extra folders (like your SQL hooks):

    python -m uvicorn main:app --reload --reload-dir path\to\hooks
    

    3. Check what’s actually imported:

    print("Loaded:", __file__)
    

    4. Kill old processes (Windows):

    taskkill /F /IM python.exe /T
    

    5. Delete all __pycache__ folders.

    6. Avoid stale imports:
    Instead of from hooks import func, use

    import hooks
    hooks.func()