[Django]-ImportError: No module named asgi

3πŸ‘

I was able to fix this; if you are using channels 2, the channel layer is quite different from the older version.

I suggest you to try the following:

  1. Confirm that you have configured your channel layer on settings.py:

    CHANNEL_LAYERS = {
        "default": {
            "BACKEND": "channels_redis.core.RedisChannelLayer",
            "CONFIG": {
                "hosts": [("redis-server-name", 6379)],
            },
        },
    }
    

    Hopefully, you have installed redis if using redis.

  2. Add the channel layer to asgi.py file:

    from channels.layers import get_channel_layer
    
    channel_layer = get_channel_layer()
    

You can see this solution.

2πŸ‘

Make sure you’ve installed it and double check you’ve activated the virtualenv.

pip install -U asgi_redis
πŸ‘€IVI

1πŸ‘

Use

pip show channels

To see the version of your package.

channels.asgi was not there until version 0.9:

https://github.com/andrewgodwin/channels/tree/0.8/channels
https://github.com/andrewgodwin/channels/tree/0.9/channels

πŸ‘€TinyX

1πŸ‘

More than likely the asgi.py file you created is in the wrong directory.

It should be in the same directory as your settings.py and wsgi.py files.

1πŸ‘

I ran into this issue when I was debugging a server and running the daphne command from the command line. I was running a command like:

/home/myuser/.virtualenvs/myapp/bin/python /home/myuser/.virtualenvs/myapp/bin/daphne -b 0.0.0.0 -p 8080  core.asgi:channel_layer

And it was still failing. Turns out I had to cd into the same directory as manage.py to get it running. If you’re using something like systemctl, you can just set the working directory to the same location.

Leave a comment