'Django: ImportError: No module named 'corsheaders'

During my first Django project i encountered a strange problem: ImportError: No module named 'corsheaders'. I have installed django-cors-headers in my virtual enviroment using

    sudo pip3 install django-cors-headers

but with on success.

pip3 freeze shows package django-cors-headers as installed, but whenever i run uwsgi it shows exception traceback in log:

 Traceback (most recent call last):
    File "./login/wsgi.py", line 16, in <module>
    application = get_wsgi_application()
    File "/home/pawel/pythonApp/myappenv/lib/python3.5/site-
    packages/django/core/wsgi.py", line 13, in get_wsgi_application
    django.setup(set_prefix=False)
    File "/home/pawel/pythonApp/myappenv/lib/python3.5/site-
    packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
    File "/home/pawel/pythonApp/myappenv/lib/python3.5/site-
    packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
    File "/home/pawel/pythonApp/myappenv/lib/python3.5/site-
    packages/django/apps/config.py", line 94, in create
    module = import_module(entry)
    File 
 "/home/pawel/pythonApp/myappenv/lib/python3.5/importlib/__init__.py", 
    line 126, in import_module
 return _bootstrap._gcd_import(name[level:], package, level)
 ImportError: No module named 'corsheaders'
 unable to load app 0 (mountpoint='') (callable not found or import 
 error)

I tried installing different corsheaders versions, but with no success either. I am running Django 1.11.7 and Python 3.5.2. Any help would be appreciated.

EDIT #1 I did follow offical install instructions from corsheader github repository. A have corsheaders in my installed apps in settings.py and coresheader middleware.



Solution 1:[1]

For anyone struggling with the same problem - running pip3 directly from my virtual enviroment:

/home/pawel/pythonApp/myappenv/bin/python3.5 pip3 install django-cors-headers

solved the problem. The issue was that while running pip3 install, module was installed in general system-wide pip3 directory, not in my virtualenv directory. To start django i was using interpreter for virtualenv, not general interpreter.

Solution 2:[2]

Suggested approach is to activate your virtual enviornment first and then install any package using pip.

Solution 3:[3]

Most likely you did not add 'corsheaders' to your INSTALLED_APPS:

INSTALLED_APPS = (
    ...
    'corsheaders',
    ...
)

Solution 4:[4]

I have solved the same problem by re install corsheaders by using this command:

python -m pip install django-cors-headers

NOT USE the bellow command(WILL GIVE YOU ERROR!!):

pip install django-cors-headers

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pawe? Matuszak
Solution 2 Amar
Solution 3 D0p
Solution 4 mayal