I’m a newbie. I’m trying to follow a basic guide to begin using FastAPI and uvicorn. However, I seem to be getting these errors, and I’m not sure why. I followed steps in the video, and wrote the same code(And file structure is the same, app.py in app folder, main.py in outer folder)
main.py
import uvicorn
if __name__ == "__main__":
uvicorn.run("app.app:app", port=8000, reload=True)
app.py
from fastapi import FastAPI
app = FastAPI()
@app.get("/", tags=['ROOT'])
async def root() -> dict:
return{"Ping":"Pong"}
Launching main.py results in the following error:
Error loading ASGI app. Attribute "app" not found in module "app.app".
I tried moving code from app.py to a different file, and launching it directly from command line using uvicorn, but got the same error
>Solution :
You should start uvicorn with app:app. The file name is already declared in the first part of app:app.
import uvicorn
if __name__ == "__main__":
uvicorn.run("app:app", port=8000, reload=True)
Also, you could run uvicorn from your terminal like so:
uvicorn app:app --port 8000 --reload