【问题标题】:Django channels cannot import name RequestAborted from exceptionsDjango 频道无法从异常中导入名称 RequestAborted
【发布时间】:2019-12-23 17:50:26
【问题描述】:

使用 daphne 这是我的设置:

PROCFILE:

 web: daphne my_application.asgi:application --port $PORT --bind 0.0.0.0 -v2

设置

 INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'django.contrib.humanize',
        'channels',
        'django_summernote',
        ....
    ]

     CHANNEL_LAYERS = {
        "default": {
            "BACKEND": "channels_redis.core.RedisChannelLayer",
            "CONFIG": {
                "hosts": [os.environ.get('REDIS_URL', 'redis://localhost:6379')],
            }
        }
    } 

    ASGI_APPLICATION = "my_application.routing.application"

路由文件:

    from channels.auth import AuthMiddlewareStack
    from channels.routing import ProtocolTypeRouter, URLRouter
    from django.conf.urls import url

    application = ProtocolTypeRouter({
        'websocket': AuthMiddlewareStack(
            URLRouter(
                [
                    url(*),
                    ....
                ]
            )
        ),
    })

ASGI.PY - 发生错误的位置

  """
    ASGI entrypoint. Configures Django and then runs the application
    defined in the ASGI_APPLICATION setting.
    """

    import os
    import django

    # HERE IT THROWS THE IMPORT ERROR
    from channels.routing import get_default_application

    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_application.settings")
    django.setup()
    application = get_default_application()  

要求:

    ...
    channels
    channels_redis
    ...

所以,使用最新的 2.(2?) 包,我相信我刚刚验证过的包具有所需的源代码。


导入错误

  from channels.exceptions import RequestAborted, RequestTimeout
  ImportError: cannot import name 'RequestAborted'

我显然有正确的包,并且可以根据源代码获得,所以 wtf 正在这里进行.....?

【问题讨论】:

    标签: python django django-views django-channels channels


    【解决方案1】:

    正如@chander 提到的:问题是你安装了 django-channels,而你已经有了频道。 解决问题运行:

    pip 卸载 django-channels

    pip 卸载渠道

    pip 安装渠道

    【讨论】:

      【解决方案2】:

      我猜你做了一个:

      pip install django-channels

      而你应该这样做

      pip 安装渠道

      安装最新版本的频道。

      我遇到了类似的问题 - 最后我把我的 venv 和 pyenv 吹走并重新安装了 - 这解决了这个问题。当我已经安装了频道时,我不小心安装了 django-channels,问题就开始了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-09-30
        • 1970-01-01
        • 2011-07-11
        • 2022-06-24
        • 1970-01-01
        • 2015-03-01
        • 2011-12-27
        • 2018-05-29
        相关资源
        最近更新 更多