Last updated: Apr 10, 2024
Reading time·3 min
The FastAPI Error loading ASGI app. Could not import module 'main' occurs when
your terminal is located in a directory that doesn't contain your FastAPI
main.py
file or the specified path is incorrect.
To solve the error, specify the path to your FastAPI file or cd
into the
directory that contains the file.
If your terminal is not located in the directory that contains your FastAPI application, you have to specify the path.
uvicorn src.main:app --reload --port 8500
Assuming your main.py
file is contained in an src
directory and your
terminal is located in the directory that contains the src
directory.
my_project <-- you are here |-- src |-- main.py
my_project
directory and your main.py
file contains your FastAPI application.Notice that we use a dot .
, not a forward slash when specifying the path to
the main.py
file that contains the FastAPI application.
uvicorn src.main:app --reload --port 8500
main.py
Alternatively, you can cd
into the directory that contains your main.py
file.
cd src uvicorn main:app --reload --port 8500
The command above assumes that your terminal is located in the same directory as
your main.py
file that contains your FastAPI application.
my_project |-- src <-- you are here |-- main.py
main:app
, assuming your FastAPI application is located in a file called main.py
.Make sure you aren't specifying the extension of the file when issuing the
uvicorn
command.
The components of the command uvicorn main:app --reload
are:
main
- the file main.py
.app
- the object created inside of main.py
with the line
app = FastAPI()
.--reload
- restart the server after code changes (should only be used in
development).main.py
fileThe example above assumes that you have a main.py
file that looks something
like this.
from typing import Union from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): return {"Hello": "World"} @app.get("/items/{item_id}") def read_item(item_id: int, q: Union[str, None] = None): return {"item_id": item_id, "q": q}
fastapi
and uvicorn
installedEnsure you have
the prerequisites installed before
running the uvicorn
command.
pip install fastapi uvicorn # 👇️ or with pip3 pip3 install fastapi uvicorn
Another common cause of the error is forgetting to save your code in your IDE (e.g. Visual Studio Code).
Make sure you aren't using a slash between the directory name and the file that contains your FastAPI application.
# ⛔️ Incorrect (has slash separator) uvicorn src/main:app --reload --port 8500 # ✅ Correct (has dot separator) uvicorn src.main:app --reload --port 8500
The separator between the folder name and the module name should be a period
(.
).
Another common cause of the error is having circular imports between files.
For example, if you have 2 files first_module.py
and second_module.py
.
If the two modules import members between each other, you have a circular import.
Instead, you should create a third file named third_module.py
and move the
members to it.
Then first_module.py
and second_module.py
can import from third_module.py
instead of importing between each other (which causes a circular import).
If you get the error ModuleNotFoundError: No module named 'fastapi', click on the link and follow the instructions.