Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

fastapi

I installed FASTAPI and was running below mentioned code

from fastapi import FastAPI
    
app = FastAPI()

@app.get("/")
def read_root():
    return {"Hello": "World"}

and am getting below mentioned error in console on executing the file

(venv) D:\FASTAPI>uvicorn main:app --reload
INFO:     Will watch for changes in these directories: ['D:\\FASTAPI']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [7900] using watchgod
ERROR:    Error loading ASGI app. Attribute "app" not found in module "main".

I really appreciate any recommendations or suggestions

like image 986
Vikas Jha Avatar asked Jun 20 '26 16:06

Vikas Jha


2 Answers

Suggestion: Make sure you have set VS Studio Code to "autosave". If updated code not saved, interpreter will not allow app startup.

like image 81
Jason Timothy Reed Avatar answered Jun 23 '26 06:06

Jason Timothy Reed


First look the project tree. The main.py file is under app directory.

In my project like this:

fastapi 
   -> app
      -> main.py

My project folder tree

Copy codes to main.py:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def root():
    return {"message": "Hello World"}

Run the server:

uvicorn app.main:app --reload

But if you try to run this server like:

uvicorn main:app --reload

This will cause the error:

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

Hope you understood. Run and enjoy coding....

like image 23
Subarata Talukder Avatar answered Jun 23 '26 05:06

Subarata Talukder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!