Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uvicorn can't find modules

I have a folder that has my venv with dependencies and a python file.

When I run the python file with python main.py it works.

When I run the python file with uvicorn main:app --reload it throws a module not found error for all my dependencies. I want to turn the main.py into a fastapi app and installed fastapi and uvicorn to my venv and did app = FASTAPI()

Why is it working when running python but uvicorn is not finding my dependencies?

I uninstalled uvicorn and reinstalled it in my venv. I was expecting it to find my dependencies in my venv folder

like image 534
Ben Weissman Avatar asked Jan 22 '26 10:01

Ben Weissman


2 Answers

I find the easiest solution for this to be to run uvicorn as a python module

python -m uvicorn main:app --reload

assuming the root of your problems is that your uvicorn main:app --reload is not respecting your venv

like image 134
alex9311 Avatar answered Jan 23 '26 22:01

alex9311


Probably you installed uvicorn outside of your virtual environment too, so when you run the command in terminal it tries to use that installation instead of the venv's version and doesn't find the dependencies.

Try calling uvicorn with the entire path, like this:

./venv/bin/uvicorn main:app --reload

Replace ./venv/ with the folder of your virtual environment.

like image 25
mferraz Avatar answered Jan 23 '26 22:01

mferraz