ImportError: cannot import name 'DEFAULT_CHANNEL_LAYER' from 'channels'

在运行Django项目时遇到ImportError,无法导入'DEFAULT_CHANNEL_LAYER' from 'channels'。检查了配置文件和manage.py中的声明,最终发现可能是channels或daphne版本不兼容导致的问题。更新到合适版本后,runserver不再报错。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

运行项目出现错误:
ImportError: cannot import name ‘DEFAULT_CHANNEL_LAYER’ from ‘channels’
我的django配置文件里是这样的

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

在manage.py中也有声明

os.environ['DJANGO_SETTINGS_MODULE'] = 'Web.settings'

最后:

pip uninstall asgi_redis
pip uninstall channels-redis
pip uninstall channels # Successfully uninstalled channels-2.1.5
pip install channels 
# Successfully uninstalled daphne-2.2.3
# 安装信息中出现daphne~=2.3
#Found existing installation: daphne 2.2.3
#    Uninstalling daphne-2.2.3:
#      Successfully uninstalled daphne-2.2.3
#Successfully installed channels-2.2.0 daphne-2.3.0

最后执行runserver没有报错了, 也许是channels,daphne版本问题造成的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值