Home:ALL Converter>simple django channels app not running with daphne

simple django channels app not running with daphne

Ask Time:2021-02-26T11:16:40         Author:Lily H.

Json Formatter

I’m trying to get django channels running with daphne but I always end up with this

x8/backback/mysite » daphne mysite.asgi:application                                                                                 1 ↵
Traceback (most recent call last):
  File "/Users/me/.local/share/virtualenvs/backback-jdouan2y/bin/daphne", line 8, in <module>
    sys.exit(CommandLineInterface.entrypoint())
  File "/Users/me/.local/share/virtualenvs/backback-jdouan2y/lib/python3.8/site-packages/daphne/cli.py", line 170, in entrypoint
    cls().run(sys.argv[1:])
  File "/Users/me/.local/share/virtualenvs/backback-jdouan2y/lib/python3.8/site-packages/daphne/cli.py", line 232, in run
    application = import_by_path(args.application)
  File "/Users/me/.local/share/virtualenvs/backback-jdouan2y/lib/python3.8/site-packages/daphne/utils.py", line 12, in import_by_path
    target = importlib.import_module(module_path)
  File "/Library/Frameworks/Python.framework/Versions/3.8/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 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "./mysite/asgi.py", line 11, in <module>
    from channels.auth import AuthMiddlewareStack
  File "/Users/me/.local/share/virtualenvs/backback-jdouan2y/lib/python3.8/site-packages/channels/auth.py", line 12, in <module>
    from django.contrib.auth.models import AnonymousUser
  File "/Users/me/.local/share/virtualenvs/backback-jdouan2y/lib/python3.8/site-packages/django/contrib/auth/models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/Users/me/.local/share/virtualenvs/backback-jdouan2y/lib/python3.8/site-packages/django/contrib/auth/base_user.py", line 48, in <module>
    class AbstractBaseUser(models.Model):
  File "/Users/me/.local/share/virtualenvs/backback-jdouan2y/lib/python3.8/site-packages/django/db/models/base.py", line 108, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/Users/me/.local/share/virtualenvs/backback-jdouan2y/lib/python3.8/site-packages/django/apps/registry.py", line 253, in get_containing_app_config
    self.check_apps_ready()
  File "/Users/me/.local/share/virtualenvs/backback-jdouan2y/lib/python3.8/site-packages/django/apps/registry.py", line 135, in check_apps_ready
    settings.INSTALLED_APPS
  File "/Users/me/.local/share/virtualenvs/backback-jdouan2y/lib/python3.8/site-packages/django/conf/__init__.py", line 82, in __getattr__
    self._setup(name)
  File "/Users/me/.local/share/virtualenvs/backback-jdouan2y/lib/python3.8/site-packages/django/conf/__init__.py", line 63, in _setup
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

I have

Django==3.1.7

daphne==3.0.1

channels==3.0.3

I successfully finished the channels setup from their site https://channels.readthedocs.io/en/stable/tutorial/part_1.html

But in the end it doesn’t work with daphne when I try a simple:

daphne mysite.asgi:application  

or

daphne -p 8001 mysite.asgi:application   

This is my current asgi.py

import os
import django
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
import chat.routing

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
django.setup()
application = ProtocolTypeRouter({
  "http": get_asgi_application(),
  "websocket": AuthMiddlewareStack(
        URLRouter(
            chat.routing.websocket_urlpatterns
        )
    ),
})

Before runserver I also tried :

export DJANGO_SETTINGS_MODULE=mysite.settings

While doing the channels tutorial, they ask us to make sure that the channel layer can communicate with Redis (part 2 of the turorial)

$ python3 manage.py shell
>>> import channels.layers
>>> channel_layer = channels.layers.get_channel_layer()
>>> from asgiref.sync import async_to_sync
>>> async_to_sync(channel_layer.send)('test_channel', {'type': 'hello'})
>>> async_to_sync(channel_layer.receive)('test_channel')
{'type': 'hello'}

If I use the python3 shell directly instead of python3 manage.py shell I get a similar error to the one I get now while trying to run daphne. I guess there is a connection, maybe, but I can't figure out where and why. I've spent days on this, doing and redoing, please help ~

Author:Lily H.,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/66379627/simple-django-channels-app-not-running-with-daphne
yy