This was quite annoying for me, so just in case someone finds this question but none of the answers above can help, here is what I did.
Add the following main method to the main file of my app:
if __name__ == "__main__":
port = os.getenv("PORT")
if not port:
port = 8080
uvicorn.run(app, host="0.0.0.0", port=8080)
Then run this using python instead of running the uvicorn
module:
python -m app.main
The output now was more useful:
Traceback (most recent call last):
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/runpy.py", line 193, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/runpy.py", line 86, in _run_code
exec(code, run_globals)
File "/Users/robert-andreidamian/Workspace/panprices/shelf-analytics-api/app/main.py", line 7, in <module>
from app.routers import auth, users, availability, data, overview
File "/Users/robert-andreidamian/Workspace/panprices/shelf-analytics-api/app/routers/overview.py", line 5, in <module>
from app.main import get_db
File "/Users/robert-andreidamian/Workspace/panprices/shelf-analytics-api/app/main.py", line 23, in <module>
app.include_router(overview.router)
AttributeError: partially initialized module 'app.routers.overview' has no attribute 'router' (most likely due to a circular import)
As you can see the problem was generated by a circular dependency issue. I ran into this when following the SQLAlchemy tutorial in the FastAPI documentation: https://fastapi.tiangolo.com/tutorial/sql-databases/ . As suggested, i created the get_db
function in my main file.
Before that I had already split the router in multiple files as explained in another section of the documentation: https://fastapi.tiangolo.com/tutorial/bigger-applications/.
This resulted in a circular dependency between the main file, that was importing the routing modules, and the routing modules that were importing the get_db
function from the main module.
Hope this saved some of your time and frustration!