FastAPI: ModuleNotFoundError: No module named 'uvicorn'
Asked Answered
R

4

9

I will admit I never used gunicorn before. When I run the command gunicorn main:app -k uvicorn.workers.UvicornWorker gives error:

Error: class uri 'uvicorn.workers.UvicornWorker' invalid or not found: 

[Traceback (most recent call last):
  File "/Users/AdnanAhmad/Data/anaconda3/lib/python3.7/site-packages/gunicorn/util.py", line 135, in load_class
    mod = import_module('.'.join(components))
  File "/Users/X/Data/anaconda3/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'uvicorn'
Rama answered 19/2, 2021 at 10:2 Comment(10)
Did you install uvicorn? (fastapi.tiangolo.com/#requirements) It isn't an explicit dependency of FastAPI or gunicorn, but it's one of the workers for running FastAPI.Overnice
@GinoMempin both uvicorn and gunicorn are installed as I am already able to use uvicorn main:app --reloadRama
@GinoMempin I found the issue. I just did which gunicorn and turns out is not using the one in my venv which sounds strange rather the one in host machine. What should I do?Rama
Uninstall the one outside the venv, you probably installed it system-wide, and the binary gunicorn is available at the beginning of your PATH. Uninstall it then make sure it's properly installed and accessible on your venv. (And make sure you are actually using the venv.)Overnice
@GinoMempin it was shipped or installed with conda.Rama
you can just run the correct one from the terminal depending on where your venv is located, i.e. env/bin/gunicorn main:app -k uvicorn.workers.UvicornWorker if your env folder is in the root of your projectArbitration
I am having the exact issue...Barton
in my case, I installed gunicorn globally with apt-get, it did not work. then I did conda install gunicorn again, this time it installed in my current conda env and it started working.Paulus
hit the same issue again 2nd time~! Adding one more case: even after I installed gunicorn I was still getting the missing uvicorn issue. Then I found uvicorn was not installed under my current conda env. so I installed it: conda install -c conda-forge uvicornPaulus
@Paulus for me it was the path issue.Rama
N
12

Check if you are calling the correct Gunicorn using which gunicorn (on Linux, or use where on Powerbash from Windows) from the terminal. If you are using a venv it should print a path pointing inside your venv directory.

It happend to me also, because I followed the instruction from the Gunicorn page and installed using sudo apt install gunicorn. It could be a good option for your production container, where you'll probably run the application without a venv, but in developer mode on you machine it will work better if you install gunicorn inside your venv directory using the pip install gunicorn, using the pip from your venv.

Then you can call it with python -m gunicorn main:app -k uvicorn.workers.UvicornWorker

Negotiation answered 29/7, 2021 at 16:48 Comment(1)
Why does it work ? Any possible explanationsLoux
M
3

The ModuleNotFoundError you're seeing means that Python cannot find the uvicorn module, which is a web server for ASGI (Asynchronous Server Gateway Interface) applications. This error can occur for a few different reasons:

  • The uvicorn module is not installed on your system. You can install it using pip install uvicorn.
  • You are running an older version of Python that does not have uvicorn installed. uvicorn requires Python 3.6 or later.
  • You are trying to import uvicorn in the wrong way. You should be able to import it by adding import uvicorn at the top of your Python file.
  • If you're still having trouble, it would be helpful to see the code you're trying to run and any additional error messages you're seeing.
Masson answered 13/12, 2022 at 6:41 Comment(0)
R
1

The FastApi docs I had to install uvicorn separately, so

This didn't work:

poetry add "fastapi[uvicorn]"
# you won't see uvicorn in the logs

This worked

poetry add fastapi uvicorn
# you can see in the logs uvicorn is installed
Ringler answered 9/8, 2022 at 8:53 Comment(0)
A
0

Try adding PYTHONPATH to your environment variables with $ export PYTHONPATH=$PWD in unix based OS. It did solve a similar issue I had.

Ales answered 6/1, 2023 at 14:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.