3

I am buidling a chat app with WebSockets and django channels. I run 'pip install channels' it gets installed successfully. Then I install 'asgi_redis'. That too gets installed. Now when I try to import channels.asgi it gives me error. Also suddenly my manage.py shell stops working. In other django projects it works fine.

Error when I try to access 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'

Everything before installing channels seems to be working fine. settings.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',
    },
}

pip freeze:

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
Gaurav Dhameeja
  • 352
  • 2
  • 13
  • Possibly an answer is here: https://stackoverflow.com/questions/21343774/importerror-no-module-named-win32api and no module named xxx means you don't have it in your python path. – misraX Oct 30 '17 at 16:20
  • Went through it before posting the question. Thank you for the reference though. And how is it that it does not detect module after i install channels? Same module works perfectly fine before doing so – Gaurav Dhameeja Oct 30 '17 at 16:25
  • Are you using virtualenv ? – misraX Oct 30 '17 at 16:26
  • VirtualEnv. I was using virtualenv previously and i got the same error. So i deleted the virtualenv and built the project again without virtualenv. Both cases same error after installing channels – Gaurav Dhameeja Oct 30 '17 at 16:28
  • Alright, please include your `pip freeze` result in the post. – misraX Oct 30 '17 at 16:33
  • And it's better to return back to virtualenv, as it will be more project specific packages and much better than system wide packages, activate the virtualenv and install django channel then run `pip freeze` from the activated virtualenv. – misraX Oct 30 '17 at 16:36
  • https://github.com/django/channels/issues/498. The exact problem I'm facing. manage.py shell is now working, however, channels don't seem to be recognised even now. – Gaurav Dhameeja Oct 30 '17 at 16:46
  • Did you follow the first link I shared, it's the exact solution @Godwin mentioned in the issue tracking comments, you do not have `pypiwin32` in your python path and `twisted` depends on it, and your `pip freeze` prove that you do not have it in your path so a simple `pip install pypiwin32` will solve this out. – misraX Oct 30 '17 at 16:50
  • Yes, but if I did it in the other directory which did not have channels, it did not give any such error. It was on my path, just occurs after installing channels. I did go through the post multiple times. – Gaurav Dhameeja Oct 30 '17 at 16:56
  • Yes, because Django by nature doesn't depend on twisted, and by not installing channel in that directory, `manage.py shell` will not call `win32api` and will work just fine. – misraX Oct 30 '17 at 17:01
  • And your python path is different in meaning than your project path, Python path in your case is in `C:\Users\gdhameeja\AppData\Local\Programs\Python\Python36` which has all your system wide python libs and site-packages, `win32api` module wasn't in your installed lib list so it is not in your python path as your `pip freeze` showed, and now if your run `pip freeze` you will find `pypiwin32` listed and `win23api` will be available for any call in system wide python packages.. Hope you got it, and best of luck :) – misraX Oct 30 '17 at 17:09
  • Thank you so much :) . M a beginner though. – Gaurav Dhameeja Oct 30 '17 at 18:08

1 Answers1

5

https://github.com/django/channels/issues/498 Refer this post. After you get that error, manually install pypiwin32 package.

pip install pypiwin32

Even though your ide won't recognise channels to import from, ignore it and runserver. It'll work.

Gaurav Dhameeja
  • 352
  • 2
  • 13