FastAPI Error loading ASGI app. Could not import module 'main'

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
3 min

banner

# FastAPI Error loading ASGI app. Could not import module 'main'

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.

fastapi error loading asgi app could not import module

# Specifying the path when running the command

If your terminal is not located in the directory that contains your FastAPI application, you have to specify the path.

shell
uvicorn src.main:app --reload --port 8500

specify full path to fastapi app

Assuming your main.py file is contained in an src directory and your terminal is located in the directory that contains the src directory.

shell
my_project <-- you are here |-- src |-- main.py
The command above would work if your terminal is located in the 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.

shell
uvicorn src.main:app --reload --port 8500

# Change your terminal to the directory that contains main.py

Alternatively, you can cd into the directory that contains your main.py file.

shell
cd src uvicorn main:app --reload --port 8500

cd into app directory

The command above assumes that your terminal is located in the same directory as your main.py file that contains your FastAPI application.

shell
my_project |-- src <-- you are here |-- main.py
You only have to specify 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).

# An example main.py file

The example above assumes that you have a main.py file that looks something like this.

main.py
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}

# Make sure you have fastapi and uvicorn installed

Ensure you have the prerequisites installed before running the uvicorn command.

shell
pip install fastapi uvicorn # 👇️ or with pip3 pip3 install fastapi uvicorn

# Make sure you have saved the changes in your IDE

Another common cause of the error is forgetting to save your code in your IDE (e.g. Visual Studio Code).

# Don't use a slash between your directory name and the file name

Make sure you aren't using a slash between the directory name and the file that contains your FastAPI application.

shell
# ⛔️ 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 (.).

# Make sure you don't have circular imports

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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.