The causes could be:
- 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) - Installing on other environments. Most of the time it happens when you use both
CondaandPiPon you system. - If you run the code from VS Code, check if the
same interpreteris 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 OverflowThe causes could be:
- 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) - Installing on other environments. Most of the time it happens when you use both
CondaandPiPon you system. - If you run the code from VS Code, check if the
same interpreteris selected.
Verify the installation:
$ pip list | grep fastapi
Suggestions:
OPT Virtual Environments to avoid these troubles.
Thank you!
Addtional, don't forget using LazyVim extra Python config.
I have the same question when using LazyVim, the solution is venv.
python3 -m venv .venv
source .venv/bin/activate
pip install fastapi
After these commands, the fastapi is correctly imported with LSP.

Can't find FastAPI module?
Having problems, specifically with imports not resolved.
python - FastAPI error: Cannot import name 'FastApi' from 'fastapi' - Stack Overflow
Import Error in Main.py
Videos
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?
They are called Pipβs I believe. Not totally sure if I need to download them to my computer or if I can copy and paste the Bash into the line of code. I created the program with the help of YouTube but unfortunately I canβt find a video that helps with this problem. Been working at it for a few days now with no resolution lol so anything helps, thanks
That should be as follows (with API being capitalized at the end). Please have a look at the documentation as well.
from fastapi import FastAPI
^^^
Additionally, make sure not to name your python script file fastapi.py, as this would interfere with the library (when adding from fastapi import FastAPI), but rather use some neutral name, such as app.py.
Are you using VS Code?
Yes? Then you may want to do the following:
ctrl+shift+p then search for 'Python:Select Interpreter', now you get to choose the desired environment where you have installed fastapi.
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.
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
Β» pip install fastapi
I have created a fastAPI route, but it is always giving me 404 always.
from fastapi import FastAPI
from .routers import auth
app = FastAPI()
app.include_router(auth.router)
@app.get("/hello")
async def read_user_me():
return {"username": "fakecurrentuser"}
@app.get("/hi")
async def root():
return {"message": "Hello Bigger Applications!"}