Greg
Greg

Reputation: 45

Python, fastAPI, uvicorn - Error loading ASGI app. Could not import module "main"

I am having trouble with getting uvicorn to start. I am very new to python and fastapi so I am assuming I have doing something very silly.

I have isolated the problem to being in my api_router.py file

from fastapi import APIRouter
from API.endpoints import user_endpoints

api_router = APIRouter()

api_router.include_router(user_endpoints, prefix="/user", tags=["User"])

When I comment out from API.endpoints import user_endpoints and api_router.include_router(user_endpoints, prefix="/user", tags=["User"]), the error does not occur. Am I trying to import my user_endpoints.py file incorrectly? I have attached an image of the directory structure.

user_endpoints.py looks like this:

from fastapi.routing import APIRouter
from typing import Optional, List
from API.schema.user import User
from models.user import Users
from main import db

router = APIRouter

@router.get('/users', response_model=List[User], status_code=200)
def get_all_users():
    users=db.query(Users).all()
    return users

@router.get('/users/{user_id}')
def get_user(user_id):
    pass
    
@router.post('/users')
def create_user():
    pass

@router.put('/users/{user_id}')
def update_user(user_id):
    pass

@router.delete('/users/{user_id}')
def delete_user(user_id):
    pass

enter image description here

Any help with this would be greatly appreciated.

Thanks,

Greg

Upvotes: 3

Views: 4775

Answers (1)

extDependency
extDependency

Reputation: 117

I think it's talking about the current working directory of your terminal, when you feed it uvicorn main:app ... not being able to find main. Make your terminal's working directory same as main.py

Upvotes: 3

Related Questions