Search code examples
pythonfastapiuvicornfastapiusers

Start Uvicorn with fastapi having main.py and app.py


I was making an authentication backend with Fastapi following the user's guide and don't know how to start a server now.

Files and folders of the project structure are the following:

<project_name>/
    main.py
    app/
        app.py

The content of the main.py script:

import uvicorn

if __name__ == "__main__":
    uvicorn.run("app.app:app", host="0.0.0.0", log_level="info")

The content of the app/app.py script:

from fastapi import Depends, FastAPI

from app.db import User, create_db_and_tables
from app.schemas import UserCreate, UserRead, UserUpdate
from app.users import auth_backend, current_active_user, fastapi_users

app = FastAPI()

# Rest of the code...

And when I'm trying to start a server with the command from my <project_name>/ folder:

uvicorn main:app --reload

It gives me this error message:

ERROR: Error loading ASGI app. Attribute "app" not found in module "main".

How to start the server in this case?


Solution

  • try running uvicorn app.app:app from <project_name>/ (don't think your main.py is necessary)