The causes could be:

  1. The name of the file - fastapi.py, if you name it in this could way you would get import errors. (Avoid filenames similar to package names)
  2. Installing on other environments. Most of the time it happens when you use both Conda and PiP on you system.
  3. If you run the code from VS Code, check if the same interpreter is selected.

Verify the installation:

$ pip list | grep fastapi

Suggestions:

OPT Virtual Environments to avoid these troubles.

Thank you!

Answer from Mahimai Raja J on Stack Overflow
Discussions

Import FastAPI does not work.
First check I added a very descriptive title to this issue. I used the GitHub search to find a similar issue and didn't find it. I searched the FastAPI documentation, with the integrated search. I ... More on github.com
🌐 github.com
4
1
March 14, 2021
python - FastAPI error: Cannot import name 'FastApi' from 'fastapi' - Stack Overflow
I want to use FastAPI. I installed it using pip, and when I am adding the package to my project and running the app like this: from fastapi import FastApi I am getting this error: cannot import... More on stackoverflow.com
🌐 stackoverflow.com
Can't find FastAPI module?
This could happen when you have fastapi installed globally (outside of the venv) and uvicorn is in the venv or vice versa , make sure you only have the packages in the venv only More on reddit.com
🌐 r/FastAPI
6
1
October 29, 2021
Import Error in Main.py
First Check I added a very descriptive title to this issue. I used the GitHub search to find a similar issue and didn't find it. I searched the FastAPI documentation, with the integrated search. I ... More on github.com
🌐 github.com
5
December 15, 2021
🌐
Medium
medium.com β€Ί @littechie β€Ί troubleshooting-import-module-error-in-python-a-solution-that-worked-901e2e2efb1b
Troubleshooting β€œImport Module” Error in Python: A Solution That Worked | by Lit Techie | Medium
July 26, 2023 - By creating a fresh virtual environment and reinstalling the required dependencies, I successfully resolved the β€œImport β€˜module’ could not be resolved from source PylancereportMissingModuleSource” error that was preventing me from importing ...
🌐
Sentry
sentry.io β€Ί sentry answers β€Ί vs code β€Ί fix pylance resolvemissingimports in vs code
Fix Pylance resolveMissingImports in VS Code | Sentry
July 15, 2024 - Select the Python interpreter in your virtual environment – it should be the top option, with a β€œRecommended” tag. If you previously installed fastapi to the virtual environment, the resolveMissingImports error should now disappear.
🌐
Bobby Hadz
bobbyhadz.com β€Ί blog β€Ί python-no-module-named-fastapi
ModuleNotFoundError: No module named 'fastapi' in Python | bobbyhadz
April 10, 2024 - To solve the error, install the module by running the pip install fastapi command. Open your terminal in your project's root directory and install the fastapi module. ... Copied!# πŸ‘‡οΈ In a virtual environment or using Python 2 pip install ...
🌐
GitHub
github.com β€Ί zed-industries β€Ί zed β€Ί discussions β€Ί 32362
Python imports don't work in virtual enviroments Β· zed-industries/zed Β· Discussion #32362
I am working on a project in a venv, and am importing fastapi. In VSCode, everything works fine, but in Zed I was getting the error Import "fastapi" could not be resolved (Pyright reportMissingImports). I Googled it, and found that I needed to configure Pyright settings.
Author Β  zed-industries
🌐
Sling Academy
slingacademy.com β€Ί article β€Ί resolving-fastapi-error-could-not-import-module-api
Resolving FastAPI Error: β€˜Could not import module β€˜api” - Sling Academy
This results in an error because neither module can be fully imported without the other. Identifying and restructuring the code to remove circular dependencies can resolve the error. Review your api module and any other modules it imports to identify circular references.
Find elsewhere
🌐
DEV Community
dev.to β€Ί drsimplegraffiti β€Ί fast-api-part-1--j2l
Fast Api [part 1 ] - DEV Community
March 31, 2022 - from fastapi import FastAPI app = FastAPI() @app.get("/") async def root(): return {"message": "Hello World"} The above is the most basic version of our restapi. We now have a problem because we use vscode and our Python interpreter cannot be found.
🌐
Reddit
reddit.com β€Ί r/fastapi β€Ί can't find fastapi module?
r/FastAPI on Reddit: Can't find FastAPI module?
October 29, 2021 -

Please help, I must be doing something dumb.

I've been using FastAPI at work for a few weeks, and enjoying it. I wanted to try it on a personal project, so I started a new repo and tried to run Hello World.

This is the whole text of main.py:

import uvicorn
from fastapi import FastAPI

app = FastAPI()

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

if __name__ == "__main__":
uvicorn.run("main:app", host="127.0.0.1", port=5000, log_level="info")

(The space after the @ sign is to stop reddit treating that as a username, it doesn't exist in the source file.)

I can run this from the command line with the Python command, and it works fine:

python3 main.py

But if I try to run it with uvicorn, I get a long message that ends with:

from fastapi import FastAPI

ModuleNotFoundError: No module named 'fastapi'

I created a virtual environment when I started the project, and if I deactivate it, I get the same error using the Python command line that I get using the uvicorn command line. When I restart the virtual environment, the Python command line works fine.

So I guess I need to let uvicorn see the modules that exist in the virtual environment, right? How can I do that?

🌐
PyTutorial
pytutorial.com β€Ί fix-python-importerror-no-module-named-fastapi
PyTutorial | Fix Python ImportError: No Module Named FastAPI
December 1, 2025 - This is a ModuleNotFoundError. Python's import system failed. The fastapi package is missing. Your code cannot proceed. The most common reason is simple. You did not install FastAPI. You must install it with pip.
🌐
Sentry
sentry.io β€Ί sentry answers β€Ί fastapi β€Ί modulenotfounderror when working with fastapi in python
ModuleNotFoundError when working with FastAPI in Python | Sentry
February 15, 2024 - The best way to solve these issues and ensure that our project runs consistently on different systems is to create a virtual environment and use that environment every time we run the code in our project.
🌐
PyTutorial
pytutorial.com β€Ί fix-fastapi-errors-common-issues-and-solutions
PyTutorial | Fix FastAPI Errors: Common Issues and Solutions
December 2, 2025 - FastAPI is a powerful modern framework. Yet, developers face common errors. This guide helps you fix them fast. We cover setup, routing, and data issues. Each section has a clear error, cause, and working solution. This error happens during startup. It breaks your application immediately. It often stems from circular imports. Python cannot resolve the dependency chain.
🌐
GitHub
github.com β€Ί fastapi β€Ί fastapi β€Ί issues β€Ί 4281
Import Error in Main.py Β· Issue #4281 Β· fastapi/fastapi
December 15, 2021 - I already checked if it is not related to FastAPI but to ReDoc. ... from sqlalchemy.orm import Session from .
Author Β  fastapi
🌐
Bobby Hadz
bobbyhadz.com β€Ί blog β€Ί python-fastapi-error-loading-asgi-app-could-not-import-module
FastAPI Error loading ASGI app. Could not import module 'main' | bobbyhadz
Make sure you aren't using a slash between the directory name and the file that contains your FastAPI application. ... Copied!# ⛔️ Incorrect (has slash separator) uvicorn src/main:app --reload --port 8500 # βœ… Correct (has dot separator) ...
🌐
GitHub
github.com β€Ί fastapi β€Ί fastapi β€Ί issues β€Ί 2951
Import FastAPI does not work. Β· Issue #2951 Β· fastapi/fastapi
March 14, 2021 - I already checked if it is not related to FastAPI but to Swagger UI.
Author Β  fastapi
Top answer
1 of 16
346

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.
2 of 16
25

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 /
Currently auto-completion in the terminal suggests the wrong format.


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) 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
🌐
Medium
medium.com β€Ί @akkaya064 β€Ί resolving-virtual-environment-and-package-issues-in-python-on-macos-f5d9d27c2d2d
Resolving Virtual Environment and Package Issues in Python on macOS | by Fatih Akkaya | Medium
March 10, 2025 - (.venv) user@user-MacBook-Pro FabricManagement % python -c "import fastapi; print(f'FastAPI Version: {fastapi.__version__}')" Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'fastapi' Even though pip list showed that FastAPI was installed, Python could not recognize the package.