ModuleNotFoundError: No module named 'win32api' right after I install channels - django

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

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.

Related

Django Error "ImproperlyConfigured: The app label 'fontawesome-free' is not a valid Python identifier."

I am trying to host a website using django and I installed fontawesome-free[version=5.15.3] which I used in my project but when I am trying to run python manage.py makemigrations I am getting this error ImproperlyConfigured: The app label 'fontawesome-free' is not a valid Python identifier. How do I fix this problem?
Full traceback:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/guitarwebsite/.virtualenvs/my-virtualenv/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/home/guitarwebsite/.virtualenvs/my-virtualenv/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
django.setup()
File "/home/guitarwebsite/.virtualenvs/my-virtualenv/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/guitarwebsite/.virtualenvs/my-virtualenv/lib/python3.8/site-packages/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "/home/guitarwebsite/.virtualenvs/my-virtualenv/lib/python3.8/site-packages/django/apps/config.py", line 255, in create
return app_config_class(app_name, app_module)
File "/home/guitarwebsite/.virtualenvs/my-virtualenv/lib/python3.8/site-packages/django/apps/config.py", line 38, in __init__
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: The app label 'fontawesome-free' is not a valid Python identifier.
As #AKX said, this is a known issue for fontawesome-free, fontawesome-pro + django >= 3.2.
This quick-fix helped.
Just before the INSTALLED_APPS add this
import sys
sys.modules['fontawesome_free'] = __import__('fontawesome-free')
or
sys.modules['fontawesome_pro'] = __import__('fontawesome-pro')
and then add "fontawesome_free" or "fontawesome_pro" to your INSTALLED_APPS
INSTALLED_APPS = [
.........
"fontawesome_free",
#or
"fontawesome_pro",
.........
]
Semi fixed as of May 21st. It's fixed with renaming the package to fontawesomefree. Here is the github issue for more info
This is a known issue in the fontawesome-free Python package, rendering it unusable as a Django app for the time being: https://github.com/FortAwesome/Font-Awesome/issues/17801

ImportError: cannot import name _remove_dead_weakref in django virtualenv

After upgrading my Ubuntu desktop to 18.04 from 16.04, the django's virtualenv refuses to start:
(.djangoenv) mw#desktop:~/theapp$ python manage.py runserver
Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
File "/home/mw/.djangoenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 10, in <module>
from django.apps import apps
File "/home/mw/.djangoenv/local/lib/python2.7/site-packages/django/apps/__init__.py", line 1, in <module>
from .config import AppConfig # NOQA
File "/home/mw/.djangoenv/local/lib/python2.7/site-packages/django/apps/config.py", line 4, in <module>
from django.core.exceptions import AppRegistryNotReady, ImproperlyConfigured
File "/home/mw/.djangoenv/local/lib/python2.7/site-packages/django/core/exceptions.py", line 5, in <module>
from django.utils.encoding import force_text
File "/home/mw/.djangoenv/local/lib/python2.7/site-packages/django/utils/encoding.py", line 10, in <module>
from django.utils.functional import Promise
File "/home/mw/.djangoenv/local/lib/python2.7/site-packages/django/utils/functional.py", line 1, in <module>
import copy
File "/usr/lib/python2.7/copy.py", line 52, in <module>
import weakref
File "/usr/lib/python2.7/weakref.py", line 14, in <module>
from _weakref import (
ImportError: cannot import name _remove_dead_weakref
On the djangoenv and outside it
$ python --version :
`Python 2.7.12`
Hoping to resolve the issue, I also install Python 2.7.16 on the desktop, so I get:
$ python2.7 --version
Python 2.7.16
Also when I try to install new virtualenv, I get the same error:
$ virtualenv .blaenv
Running virtualenv with interpreter /home/mw/.djangoenv/bin/python2
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/virtualenv.py", line 21, in <module>
import logging
File "/usr/lib/python2.7/logging/__init__.py", line 26, in <module>
import sys, os, time, cStringIO, traceback, warnings, weakref, collections
File "/usr/lib/python2.7/weakref.py", line 14, in <module>
from _weakref import (
ImportError: cannot import name _remove_dead_weakref
Answers to the same error in other contexts did not help and my search led to no more clues.
So really appreciate your hints.
As per https://askubuntu.com/questions/981663/python2-7-broken-by-weakref-import-error-please-help you probably need to recreate your virtualenv. I suspect your problem here is that you need to deactivate your current env before you try to make the new one, in order to ensure that your system python2 interpreter is the one running the virtualenv command.

Issue with importing rest_framework inside django app

I am facing an issue of importing rest_framework inside my django app whenever i try to make migrations or create superuser or simply run the runserver.
I have installed the framework using this command but django still doesn't recognize it
sudo pip install djangorestframework
here's the snippet of settings.py file:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'companies.apps.CompaniesConfig',
]
REST_FRAMEWORK = {
'DEFAULT_MODEL_SERIALIZER_CLASS':
'rest_framework.serializers.HyperlinkedModelSerializer',
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
]
}
Output:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/tam/Enthought/Canopy_64bit/User/lib/python2.7/site-packages
/django/core/management/__init__.py", line 364
in execute_from_command_line utility.execute()
File "/home/tam/Enthought/Canopy_64bit/User/lib/python2.7/site-packages
/django/core/management/__init__.py", line 338, in execute
django.setup()
File "/home/tam/Enthought/Canopy_64bit/User/lib/python2.7/site-packages
/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/tam/Enthought/Canopy_64bit/User/lib/python2.7/site-packages
/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/home/tam/Enthought/Canopy_64bit/User/lib/python2.7/site-packages
/django/apps/config.py", line 94, in create
module = import_module(entry)
File "/home/tam/Canopy/appdata/canopy-1.7.4.3348.rh5-x86_64/lib/python2.7/
importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named rest_framework

Import Error: No module named django - for specific project

I have installed Django(1.8.2) in my Ubuntu 16.04.
When I cloned a working project into it and run the server, I got the following error.
Traceback (most recent call last):
File "manage.py", line 31, in <module>
execute_from_command_line(sys.argv)
File "/usr/lib/python2.7/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/lib/python2.7/django/core/management/__init__.py", line 312, in execute
django.setup()
File "/usr/lib/python2.7/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/lib/python2.7/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/usr/lib/python2.7/django/apps/config.py", line 119, in create
import_module(entry)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named django
Observation(s):
I'm able to start a new project and run the server of the same.
The value of INSTALLED_APPS in settings.py is:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'Telecommands',
'Telemetry',
'captcha',
'django.contrib.sitemaps',
'djcelery',
'kombu.transport.django',
# 'grappelli',
# 'chronograph',
# 'registration', # Include the registration
)
What have I tried?
Installing django using pip install django==1.8.2.
Since the error was reported with reference to /usr/lib/python2.7/,
I tried:
sudo pip install --install-option="--install-purelib=/usr/lib/python2.7/site-packages/" --ignore-installed django==1.8.2
Further, when I got confused with paths
/usr/local/lib/python2.7/dist-packages,
/usr/lib/python2.7/dist-packages/
and ~/.local/lib/python2.7/dist-packages.
I installed django(1.8.2) to each of these paths one by one,
updating PYTHONPATH in parallel.
I did not get any errors while installing.
Can somebody help me out. Kindly explain the reason for the error as well.
The traceback shows you the error occurs in /usr/lib/python2.7/django/__init__.py, so manage.py has clearly found your Django installation. As an aside, it would be much better to use a virtual env instead of installing in /usr/lib/python2.7/site-packages/.
The problem appears to be that you have kombu.transport.django in your INSTALLED_APPS. The Django transport was removed from kombu in 4.0.

ImportError: cannot import name RequestSite when inlude 'registration' in installed apps

I am trying to install and run Django-registration-redux and whenever I try to include 'registration' in installed apps I am getting an error:
Traceback (most recent call last):
File "C:\Users\AdamSmith\projects\myvenv\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "C:\Users\AdamSmith\projects\myvenv\lib\site-packages\django\core\management\commands\runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "C:\Users\AdamSmith\projects\myvenv\lib\site-packages\django\utils\autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "C:\Users\AdamSmith\projects\myvenv\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "C:\Users\AdamSmith\projects\myvenv\lib\site-packages\django\__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\AdamSmith\projects\myvenv\lib\site-packages\django\apps\registry.py", line 115, in populate
app_config.ready()
File "C:\Users\AdamSmith\projects\myvenv\lib\site-packages\django\contrib\admin\apps.py", line 22, in ready
self.module.autodiscover()
File "C:\Users\AdamSmith\projects\myvenv\lib\site-packages\django\contrib\admin\__init__.py", line 26, in autodiscover
autodiscover_modules('admin', register_to=site)
File "C:\Users\AdamSmith\projects\myvenv\lib\site-packages\django\utils\module_loading.py", line 50, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "c:\python27\Lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "C:\Users\AdamSmith\projects\myvenv\lib\site-packages\registration\admin.py", line 2, in <module>
from django.contrib.sites.models import RequestSite
ImportError: cannot import name RequestSite
I have freshly installed Django in myenv virtualenv. manage.py runserver is working fine and loading admin page perfectly without 'registration' app included. FYR: I am using python27 and here is my pip freeze for myenv:
> pip freeze
Django==1.9
django-registration-redux==1.2
wheel==0.24.0
and settings.py inludes the following apps:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'registration',
]
Do you have any idea on what is the problem here?
I ran into the same error (take a look at the docs, Site is under django.contrib.sites.models and RequestSite under django.contrib.sites.requests). Replace :
from django.contrib.sites.models import RequestSite
by
from django.contrib.sites.requests import RequestSite
in [YourPath]/admin.py, (it's C:\Users\AdamSmith\projects\myvenv\lib\site-packages\registration\admin.py in your case).
This issue is fixed in django-registration-redux 1.3. See https://github.com/macropin/django-registration/issues/132.