简体   繁体   中英

FastAPI: ModuleNotFoundError: No module named 'uvicorn'

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'

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

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM