2

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 ~

Lily H.
  • 164
  • 2
  • 10
  • 2
    The error seems to originate from the line where `AuthMiddlewareStack` is imported, try moving the call to `os.environ.setdefault` above it? – Iain Shelvington Feb 26 '21 at 03:20
  • Hi Lain, thanks for your time. I tried your recommendation and I get similar error but the last lines look like : `File "/Users/me/.local/share/virtualenvs/backback-jdouan2y/lib/python3.8/site-packages/django/apps/registry.py", line 136, in check_apps_ready raise AppRegistryNotReady("Apps aren't loaded yet.") django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.` – Lily H. Feb 26 '21 at 03:29
  • 2
    oh gosh it's working ! I tried to tweak around with your recommendation and set my asgi.py as such : `import os import django os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings") django.setup() from channels.auth import AuthMiddlewareStack...` and it's a succes!!!!! thanks so much for your insight !!! – Lily H. Feb 26 '21 at 03:42
  • Hi @LilyH., Can you include the correct configuration that works? – Durodola Opemipo Jun 22 '21 at 15:41
  • In my case, I moved 'channels' up in my INSTALLED_APPS. – Durodola Opemipo Jun 22 '21 at 15:45

1 Answers1

5

yes guys, in your asgi.py, you have to move the following code just before your "from channels.auth import AuthMiddlewareStack":

os.environ.setdefault('DJANGO_SETTINGS_MODULE', '****.settings')
django.setup()

see the image https://i.stack.imgur.com/szSw4.jpg

user16601763
  • 51
  • 1
  • 2