channels icon indicating copy to clipboard operation
channels copied to clipboard

ImportError: cannot import name 'DEFAULT_CHANNEL_LAYER'

Open lexygon opened this issue 10 years ago • 17 comments

I cannot say I'm good at submitting issues etc. But I'm going to try it. So I hope this will help. I just faced an error when I'm trying to run the databinding example which is in your channels-examples repo.

My channels version is 0.17.2 and I installed it from pip. Here is the error:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/usr/lib/python3.5/site-packages/django/core/management/__init__.py", line 341, in execute
    django.setup()
  File "/usr/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/usr/lib/python3.5/site-packages/django/apps/config.py", line 199, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/lexygon/Downloads/channels-examples-master/databinding/values/models.py", line 2, in <module>
    from channels.binding.websockets import WebsocketBinding
  File "/usr/lib/python3.5/site-packages/channels/binding/__init__.py", line 1, in <module>
    from .base import Binding  # NOQA isort:skip
  File "/usr/lib/python3.5/site-packages/channels/binding/base.py", line 8, in <module>
    from ..channel import Group
  File "/usr/lib/python3.5/site-packages/channels/channel.py", line 5, in <module>
    from channels import DEFAULT_CHANNEL_LAYER, channel_layers
ImportError: cannot import name 'DEFAULT_CHANNEL_LAYER'

lexygon avatar Aug 12 '16 03:08 lexygon

That is very unusual indeed. Could you run this in a python shell in the same environment and paste the result here?

import channels; print(dir(channels))

andrewgodwin avatar Aug 12 '16 04:08 andrewgodwin

Here it is:

Python 3.5.2 (default, Jun 28 2016, 08:46:01) 
[GCC 6.1.1 20160602] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import channels; print(dir(channels))
['ImproperlyConfigured', '_BACKENDS', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_load_backend', '_load_django_settings', '_load_module', '_send', 'absolute_import', 'exceptions', 'send', 'sys', 'unicode_literals']
>>> 

lexygon avatar Aug 12 '16 12:08 lexygon

That's not the main Channels package you're importing there. Could do you this to see what it is?

import channels; print(channels.__file__)

andrewgodwin avatar Aug 13 '16 00:08 andrewgodwin

Here it is. I forgot to say I'm not using it in a virtualenv. I hope it is not necessary :)

Python 3.5.2 (default, Jun 28 2016, 08:46:01) 
[GCC 6.1.1 20160602] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import channels; print(channels.__file__)
/usr/lib/python3.5/site-packages/channels/__init__.py
>>> 

edit: I installed Channels in a virtualenv and checked the files. Everything okay. Codes are same with github. But when I install it from pip without virtualenv the files are not same. There is a "backend" folder and many of codes are different etc. But I don't know why this happens. I think it is about pip

lexygon avatar Aug 13 '16 17:08 lexygon

Did you by chance use pip install django_channels? I did the same at first. Try pip install channels.

This other project shares the same namespace: https://github.com/ymyzk/django-channels

globophobe avatar Aug 14 '16 01:08 globophobe

Closing, since I suspect that's exactly what happened.

andrewgodwin avatar Aug 26 '16 04:08 andrewgodwin

Happened to me too, if channels is properly installed a message saying ASGI/Channels development server is runnning should appear in your logs, not development server.

yingw787 avatar May 28 '20 19:05 yingw787

try changing your settings add 'daphne' instead of channels

Akarsh711 avatar Nov 17 '22 14:11 Akarsh711

In my case, I installed channels and daphne separately, which was causing conflicts. As per docs it should be placed like below.

Make sure to remove channels and daphne first.

pip uninstall channels

pip uninstall daphne

Now, Install both channels and daphne together with the below command

python -m pip install -U channels["daphne"]

in settings.py

INSTALLED_APPS = (
    "daphne",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.sites",
    ...
)

YashMarmat avatar Dec 23 '22 08:12 YashMarmat

@YashMarmat you saved lives

nurettin avatar Jan 23 '24 07:01 nurettin

@YashMarmat Thanks guyz i was about to give up

AlfredBaraka avatar Mar 28 '24 08:03 AlfredBaraka

I actually had to uninstall both django-channels and channels and then install channels again.

bubikhonza avatar May 27 '24 16:05 bubikhonza

@YashMarmat I was about to suicide then i saw this github. Really saved lifess

Meghvarna-Dev avatar Jan 16 '25 07:01 Meghvarna-Dev

@YashMarmat you really saved life's

zuber74 avatar Feb 26 '25 12:02 zuber74

I actually had to uninstall both django-channels and channels and then install channels again. solved it for me

bilalgoksui avatar Apr 22 '25 10:04 bilalgoksui

Adding to this: When managing dependencies with poetry, uninstalling django-channels and then installing channels did not help - I think the package contents were not being properly overwritten. Deleting the entire virtualenv and recreating it helped.

  1. poetry env list
  2. poetry env remove <env>
  3. poetry install

matyasrichter avatar May 09 '25 06:05 matyasrichter

thanks for clarifying this problem, worked for me

deblandns avatar Dec 13 '25 19:12 deblandns