Report this

What is the reason for this report?

Django application deployment with ASGI server

Posted on February 21, 2022

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!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

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:

Check Installed Versions

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.


2. Update asgiref

Ensure 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

3. Update uvicorn

Ensure you have the latest version of uvicorn, as newer versions often fix compatibility issues:

pip install --upgrade uvicorn

4. Reinstall Gunicorn with Uvicorn Worker

If the issue persists, reinstall gunicorn and uvicorn to ensure everything is properly installed:

pip install --force-reinstall gunicorn uvicorn

5. Verify Compatibility

Check the compatibility of all related packages:

  • Django: Ensure you are using a version compatible with ASGI and asgiref.
  • uvicorn: Compatible with the installed version of asgiref.
  • asgiref: Updated to at least 3.6.0.

6. Retry the Command

Run the gunicorn command again:

gunicorn --bind 0.0.0.0:8000 myproject.asgi:application -w 4 -k uvicorn.workers.UvicornWorker

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

Dark mode is coming soon.