You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:运行Django项目报错ModuleNotFoundError: 找不到channels.http模块

问题:ModuleNotFoundError: No module named 'channels.http'

我查了不少类似问题都没解决,已经尝试更新asgiref、channels和django-eventstream版本,主要参照django-eventstream官方配置文档来设置的。

环境包版本

Package            Version
------------------ ---------
asgiref            3.6.0
channels           4.0.0
Django             4.1.2
django-eventstream 4.5.1
django-grip        3.2.0
django-htmx        1.12.2
gripcontrol        4.1.0
huey               2.4.3
MarkupSafe         2.1.2
requests           2.28.1
Werkzeug           2.2.2

运行python manage.py runserver时的错误

...
File "...\venv\lib\site-packages\django_eventstream\routing.py", line 2, in <module>
    from . import consumers
File "...\venv\lib\site-packages\django_eventstream\consumers.py", line 7, in <module>
    from channels.http import AsgiRequest
ModuleNotFoundError: No module named 'channels.http'

自定义的asgi.py代码

(文件位于settings.py同级目录)

import os
from django.core.asgi import get_asgi_application
from django.urls import path, re_path
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
import django_eventstream

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'channels_test.settings')

application = ProtocolTypeRouter({
    'http': URLRouter([
        path("events/", AuthMiddlewareStack(URLRouter(django_eventstream.routing.urlpatterns)), { 'channels': ['test'] }),
        re_path(r"", get_asgi_application()),
    ]),
})

settings.py中的ASGI配置

WSGI_APPLICATION = "channels_test.wsgi.application"
ASGI_APPLICATION = "channels_test.asgi.application"

项目文件夹结构

文件夹结构

内容的提问来源于stack exchange,提问作者xtlc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 11:01:33