Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

5.3K
Views
FastAPI throws an error (Error loading ASGI app. Could not import module "api")

I tried to run FastAPI using uvicorn webserver but it throws an error.

I run this command,

uvicorn api:app --reload --host 0.0.0.0

but there is an error in the terminal.

Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
Started reloader process [23445]
Error loading ASGI app. Could not import module "api".
Stopping reloader process [23445]

I really appreciate any recommendations or suggestions

over 4 years ago · Santiago Trujillo
9 answers
Answer question

0

its typo in "api" check your file name.

over 4 years ago · Santiago Trujillo Report

0

TL;DR

Add the directory name in front of your filename

uvicorn src.main:app 

or cd into that directory

cd src
uvicorn main:app 

Long Answer

It happens because you are not in the same folder with your FastAPI app instance more specifically:

Let's say i have an app-tree like this;

my_fastapi_app/
├── app.yaml
├── docker-compose.yml
├── src
│   └── main.py
└── tests
    ├── test_xx.py
    └── test_yy.py

$ pwd         # Present Working Directory
/home/yagiz/Desktop/my_fastapi_app

I'm not inside the same folder with my app instance, so if I try to run my app with uvicorn I'll get an error like yours

$ uvicorn main:app --reload
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [40645] using statreload
ERROR:    Error loading ASGI app. Could not import module "main".

The answer is so simple, add the folder name in front of your filename

uvicorn src.main:app --reload

or you can change your working directory

cd src 

Now i'm inside of the folder with my app instance

src
└── main.py

Run your uvicorn again

$ uvicorn main:app --reload
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [40726] using statreload
INFO:     Started server process [40728]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
over 4 years ago · Santiago Trujillo Report

0

I had the same problem and solved it adding package name before main, in your case trying:

uvicorn src.main:app --reload

may solve the problem

over 4 years ago · Santiago Trujillo Report

0

This worked for me look at the docs for fastAPI. I am very thankful I ran accross that as the Python script needs to be named main.py not app.py

The command uvicorn main:app refers to:

  1. main: the file main.py (the Python "module").
  2. app: the object created inside of main.py with the line app = FastAPI()
  3. --reload: make the server restart after code changes. Only use for development.
over 4 years ago · Santiago Trujillo Report

0

One reason this might be happening is that you are using:

uvicorn src/main:app --reload    

instead of the correct syntax:

uvicorn src.main:app --reload 

Notice the . instead of the /


That's assuming that (1) your structure is something like this:

project_folder/
├── some_folder
├── src
│   └── main.py
└── tests
    ├── test_xx.py
    └── test_yy.py

(2) that your FastAPI() object is indeed assigned to an object named app in main.py:

app = FastAPI()

(3) you are running the uvicorn command from the project_folder, e.g.:

(venv) <username>@<pcname>:~/PycharmProjects/project_folder$ uvicorn src.main:app --reload
over 4 years ago · Santiago Trujillo Report

0

It seems it is important that you name your file main.py otherwise it won't work.

Edit: Actually I was running Jupyter Notebook on port 8888 so that port was already occupied. If you have to run Jupyter notebook, run it after running the API server, the notebook will automatically run on 8889. Alternatively, you can run the API server on a different port.

over 4 years ago · Santiago Trujillo Report

0

Hit the Save button in VS Code etc because sometimes it will throw this error if you have not saved the file. Happened to me.

over 4 years ago · Santiago Trujillo Report

0

moving the import statement for FastAPI to the top of the import statements fixes the error.

from fastapi import FastAPI "then any other import statement"

over 4 years ago · Santiago Trujillo Report

0

Use this folder structure and configuration where main is in parent directory enter image description here

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: FastAPI",
            "type": "python",
            "request": "launch",
            "module": "uvicorn",
            "args": [
                "main:app"
            ],
            "jinja": true
        }
    ]
}

main.py

from fastapi import FastAPI
app = FastAPI(
    title="test",
    description="test",
    version="0.0.1",
)
if __name__ == "__main__":
import uvicorn

uvicorn.run(
    "main:app",
    host="0.0.0.0",
    reload=True,
    port=3001,
)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!