我正在构建一个带有 WebSockets 和 django channel 的聊天应用程序。
我运行'pip install channels'它成功安装。然后我安装'asgi_redis'。这也被安装。现在,当我尝试导入 channels.asgi 时,它给了我错误。也突然我的 manage.py shell 停止工作。在其他 Django 项目中,它工作正常。

尝试访问 shell 时出错:

C:\Users\gdhameeja\Desktop\chatapp\chat>manage.py shell
Traceback (most recent call last):
  File "C:\Users\gdhameeja\Desktop\chatapp\chat\manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\gdhameeja\AppData\Local\Programs\Python\Python36\lib\site-packages\django-1.11.6-py3.6.egg\django\core\management\__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "C:\Users\gdhameeja\AppData\Local\Programs\Python\Python36\lib\site-packages\django-1.11.6-py3.6.egg\django\core\management\__init__.py", line 338, in execute
    django.setup()
  File "C:\Users\gdhameeja\AppData\Local\Programs\Python\Python36\lib\site-packages\django-1.11.6-py3.6.egg\django\__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\gdhameeja\AppData\Local\Programs\Python\Python36\lib\site-packages\django-1.11.6-py3.6.egg\django\apps\registry.py", line 116, in populate
    app_config.ready()
  File "C:\Users\gdhameeja\AppData\Local\Programs\Python\Python36\lib\site-packages\channels\apps.py", line 17, in ready
    monkeypatch_django()
  File "C:\Users\gdhameeja\AppData\Local\Programs\Python\Python36\lib\site-packages\channels\hacks.py", line 10, in monkeypatch_django
    from .management.commands.runserver import Command as RunserverCommand
  File "C:\Users\gdhameeja\AppData\Local\Programs\Python\Python36\lib\site-packages\channels\management\commands\runserver.py", line 5, in <module>
    from daphne.server import Server, build_endpoint_description_strings
  File "C:\Users\gdhameeja\AppData\Local\Programs\Python\Python36\lib\site-packages\daphne\server.py", line 9, in <module>
    from twisted.internet.endpoints import serverFromString
  File "C:\Users\gdhameeja\AppData\Local\Programs\Python\Python36\lib\site-packages\twisted\internet\endpoints.py", line 41, in <module>
    from twisted.internet.stdio import StandardIO, PipeAddress
  File "C:\Users\gdhameeja\AppData\Local\Programs\Python\Python36\lib\site-packages\twisted\internet\stdio.py", line 30, in <module>
    from twisted.internet import _win32stdio
  File "C:\Users\gdhameeja\AppData\Local\Programs\Python\Python36\lib\site-packages\twisted\internet\_win32stdio.py", line 9, in <module>
    import win32api
ModuleNotFoundError: No module named 'win32api'

安装 channel 之前的一切似乎都运行良好。
设置.py:
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'chat',
    'channels',]
CHANNEL_LAYERS = {
    'default': {
        'BACKEND' : 'asgi_redis.RedisChannelLayer',
        'CONFIG':{
            'hosts': [os.environ.get('REDIS_URL', 'redis://localhost:6379')],
        },
        'ROUTING' : 'chat.routing.channel_routing',
    },
}

点卡住:
C:\Users\gdhameeja\Desktop\chatapp\chat>pip freeze
asgi-redis==1.4.3
asgiref==1.1.2
attrs==17.2.0
autobahn==17.9.3
Automat==0.6.0
channels==1.1.8
constantly==15.1.0
daphne==1.3.0
django==1.11.6
hyperlink==17.3.1
incremental==17.5.0
msgpack-python==0.4.8
pytz==2017.2
redis==2.10.6
six==1.11.0
Twisted==17.9.0
txaio==2.8.2
virtualenv==15.1.0
websocket-client==0.44.0
zope.interface==4.4.3

最佳答案

https://github.com/django/channels/issues/498
引用这个帖子。收到该错误后,手动安装 pypiwin32 包。

pip install pypiwin32

即使您的 ide 无法识别要从中导入的 channel ,请忽略它并运行服务器。它会工作。

关于django - ModuleNotFoundError : No module named 'win32api' right after I install channels,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47019751/

10-12 18:41