I am trying to deploy Django application with ASGI server, I am following this tutorial(https://www.digitalocean.com/community/tutorials/how-to-set-up-an-asgi-django-app-with-postgres-nginx-and-uvicorn-on-ubuntu-20-04). When I run this command (gunicorn --bind 0.0.0.0:8000 myproject.asgi -w 4 -k uvicorn.workers.UvicornWorker). This is the error
Error: class uri ‘uvicorn.workers.UvicornWorker’ invalid or not found:
[Traceback (most recent call last): File “/home/sreekar/.local/lib/python3.8/site-packages/gunicorn/util.py”, line 99, in load_class mod = importlib.import_module(‘.’.join(components)) File “/usr/lib/python3.8/importlib/init.py”, line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File “<frozen importlib._bootstrap>”, line 1014, in _gcd_import File “<frozen importlib._bootstrap>”, line 991, in _find_and_load File “<frozen importlib._bootstrap>”, line 961, in _find_and_load_unlocked File “<frozen importlib._bootstrap>”, line 219, in _call_with_frames_removed File “<frozen importlib._bootstrap>”, line 1014, in _gcd_import File “<frozen importlib._bootstrap>”, line 991, in _find_and_load File “<frozen importlib._bootstrap>”, line 975, in _find_and_load_unlocked File “<frozen importlib._bootstrap>”, line 671, in _load_unlocked File “<frozen importlib._bootstrap_external>”, line 848, in exec_module File “<frozen importlib._bootstrap>”, line 219, in _call_with_frames_removed File “/home/sreekar/.local/lib/python3.8/site-packages/uvicorn/init.py”, line 1, in <module> from uvicorn.config import Config File “/home/sreekar/.local/lib/python3.8/site-packages/uvicorn/config.py”, line 35, in <module> from uvicorn.middleware.proxy_headers import ProxyHeadersMiddleware File “/home/sreekar/.local/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py”, line 13, in <module> from asgiref.typing import ( ImportError: cannot import name ‘WebSocketScope’ from ‘asgiref.typing’ (/home/sreekar/.local/lib/python3.8/site-packages/asgiref/typing.py) ]
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hell,
When you executed the following command, did it complete successfully:
pip install django gunicorn uvicorn psycopg2-binary
Also if you try to run the same command but with uvicorn does it work as normal:
uvicorn myproject.asgi:application --host 0.0.0.0 --port 8080
Best,
Bobby
The error suggests that there is a mismatch between the versions of the asgiref library and uvicorn. Specifically, uvicorn is trying to import WebSocketScope from asgiref.typing, but the WebSocketScope type is missing, likely because of a version conflict.
Here’s how to resolve this issue:
Verify the versions of uvicorn and asgiref installed in your environment:
pip show uvicorn asgiref
You’ll likely find an outdated or incompatible version of asgiref.
asgirefEnsure you have a version of asgiref compatible with uvicorn. For uvicorn versions >=0.19, asgiref >=3.6.0 is required.
Update asgiref:
pip install --upgrade asgiref
uvicornEnsure you have the latest version of uvicorn, as newer versions often fix compatibility issues:
pip install --upgrade uvicorn
If the issue persists, reinstall gunicorn and uvicorn to ensure everything is properly installed:
pip install --force-reinstall gunicorn uvicorn
Check the compatibility of all related packages:
asgiref.asgiref.3.6.0.Run the gunicorn command again:
gunicorn --bind 0.0.0.0:8000 myproject.asgi:application -w 4 -k uvicorn.workers.UvicornWorker
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.