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

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 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 throws an error (Error loading ASGI app. Could not import module "api") - Stack Overflow
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://... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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.
🌐
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.
🌐
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.
🌐
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 ...
Find elsewhere
🌐
Tutorial Reference
tutorialreference.com β€Ί python β€Ί examples β€Ί faq β€Ί python-error-modulenotfounderror-no-module-named-fastapi
How to Resolve "ModuleNotFoundError: No module named 'fastapi'" in Python | Tutorial Reference
Wrong Environment: You installed fastapi in a different Python environment (e.g., a virtual environment) than the one your script or IDE is using. Typo: You have a typo in your import statement (though this would usually be ModuleNotFoundError: No module named 'fasttapi', for instance - misspelling ...
🌐
PyTutorial
pytutorial.com β€Ί fix-python-importerror-no-module-named-fastapi
PyTutorial | Fix Python ImportError: No Module Named FastAPI
December 1, 2025 - In VS Code, open the command palette. Select "Python: Select Interpreter". Choose the Python path where FastAPI is installed. Restart your IDE after changes. This reloads the environment. The import error should disappear.
🌐
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?

🌐
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.
🌐
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 - This confirmed that Python was searching for packages in the global Python installation instead of the virtual environment. To resolve the issue, I deleted the existing virtual environment and created a new one with the correct Python version.
🌐
PyTutorial
pytutorial.com β€Ί fix-fastapi-errors-common-issues-and-solutions
PyTutorial | Fix FastAPI Errors: Common Issues and Solutions
December 2, 2025 - Be specific with origins in production. Do not use wildcard "*" for sensitive data. FastAPI errors are often simple to fix. Understanding the framework is key. Most issues relate to Python basics, async/await, or Pydantic models. Use the interactive docs. Read error details carefully. Structure your project to avoid circular imports.
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
🌐
GitHub
github.com β€Ί zed-industries β€Ί zed β€Ί discussions β€Ί 32362
Python imports don't work in virtual enviroments Β· zed-industries/zed Β· Discussion #32362
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. So I did. ... { "languages": { "Python": { "language_servers": ["pyright"] } }, "lsp": { "pyright": { "settings": { "python": { "pythonPath": ".venv/bin/python" } } } } }
Author Β  zed-industries
🌐
Markaicode
markaicode.com β€Ί home β€Ί python β€Ί fastapi modulenotfounderror: diagnose and fix in 3 steps
FastAPI ModuleNotFoundError: Diagnose and Fix in 3 Steps | Markaicode
May 30, 2026 - The error fires whenever Python cannot resolve the import, which includes: (1) FastAPI genuinely not installed in the active environment; (2) a file named fastapi.py in your project directory shadowing the library; (3) running uvicorn from a ...
🌐
GitHub
github.com β€Ί fastapi β€Ί fastapi β€Ί issues β€Ί 2951
Import FastAPI does not work. Β· Issue #2951 Β· fastapi/fastapi
March 14, 2021 - 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 already searched in Google "How to X in ...
Author Β  fastapi