Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何解决导入错误:无法导入名称';AsyncWebsocketConsumer';?_Python_Django_Chat_Django Channels - Fatal编程技术网

Python 如何解决导入错误:无法导入名称';AsyncWebsocketConsumer';?

Python 如何解决导入错误:无法导入名称';AsyncWebsocketConsumer';?,python,django,chat,django-channels,Python,Django,Chat,Django Channels,我使用的是Python 3.6、Django 1.11和chennels=2。 我将学习本教程,直到教程第2部分: 当我转到教程第3部分并相应地更改ChatConsumer文件时: 我面临着这个问题 ImportError:无法导入名称“AsyncWebsocketConsumer” Consumer.py: from channels.generic.websocket import AsyncWebsocketConsumer import json class ChatConsumer(

我使用的是Python 3.6、Django 1.11和chennels=2。 我将学习本教程,直到教程第2部分:

当我转到教程第3部分并相应地更改ChatConsumer文件时:

我面临着这个问题


ImportError:无法导入名称“AsyncWebsocketConsumer”

Consumer.py:

from channels.generic.websocket import AsyncWebsocketConsumer
import json

class ChatConsumer(AsyncWebsocketConsumer):
    async def connect(self):
        self.room_name = self.scope['url_route']['kwargs']['room_name']
        self.room_group_name = 'chat_%s' % self.room_name

        # Join room group
        await self.channel_layer.group_add(
            self.room_group_name,
            self.channel_name
        )

        await self.accept()

    async def disconnect(self, close_code):
        # Leave room group
        await self.channel_layer.group_discard(
            self.room_group_name,
            self.channel_name
        )

    # Receive message from WebSocket
    async def receive(self, text_data):
        text_data_json = json.loads(text_data)
        message = text_data_json['message']

        # Send message to room group
        await self.channel_layer.group_send(
            self.room_group_name,
            {
                'type': 'chat_message',
                'message': message
            }
        )

    # Receive message from room group
    async def chat_message(self, event):
        message = event['message']

        # Send message to WebSocket
        await self.send(text_data=json.dumps({
            'message': message
        }))
错误:
任何一种都将被感激

要将使用者函数更改为异步性质,您需要从
channels.generic.websocket

from channels.generic.websocket import AsyncWebsocketConsumer

更新到通道版本2.1,解决了此错误

pip install channels==2.1

你能展示一下你的consumers.py吗?如果你能展示完整的回溯,那就太棒了。。本教程是为支持Python 3.5+和Django 1.11+的Channel 2.0编写的。这是在教程中提到的,我认为django 1.11也是受支持的。我认为django 2 url.py不同于django 1.11,在django 1.11中,url用作url(r'^$',views.index,name='index'),在django 2中,url是:教程中的路径('admin/',admin.site.url)用作url(r'^$',views.index,name='index'))url未完全弃用,您仍然可以使用url和路径。我也用1.11检查过了,它还在工作。你能给我看一下你的
pip freeze
输出吗?在教程中,Django版本1.11.10的使用如所示,你能导入
WebsocketConsumer
?哦,你在教程2中告诉过working find。。我没看见。在
python manage.py shell
中调用
AsyncWebsocketConsuemr
时会发生什么?导入channels.layers channel\u layer=channels.layers.get\u channel\u layer()从asgiref.sync导入async\u to\u sync\u to\u sync(channel\u layer.send)('test\u channel',{'type':'hello'})异步\u-to\u-sync(channel\u-layer.receive)('test\u-channel'))我在shell中遵循了这一点,正如教程第2部分中提到的,它工作得很好。我的意思是,在shell中从channels.generic.websocket导入异步websocketconsumer会发生什么?您可以在shell中调用
AsyncWebsocketConsumer
,但不能仅在consumers.py中调用?ImportError:无法导入名称“AsyncWebsocketConsumer”,从django shell调用时出现相同错误