68

i am a beginner in django. i am doing project based on it. project has two stages. i completed first phase and uploaded the code to amazon ec2 instance. after completing second phase i added some packages like python-social-auth, django-cors-headers, django-easy-maps, crispyforms. but now it is showing import error for corsheaders and this the traceback i have checked virtual environment and corsheaders package is ther

Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 280, in execute
translation.activate('en-us')
File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/__init__.py", line 130, in activate
return _trans.activate(language)
File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 188, in activate
_active.value = translation(language)
File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 177, in translation
default_translation = _fetch(settings.LANGUAGE_CODE)
File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 159, in _fetch
app = import_module(appname)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 40, in import_module
__import__(name)
ImportError: No module named corsheaders

any suggestions please

Kevin Brown-Silva
  • 40,873
  • 40
  • 203
  • 237
shebeer
  • 683
  • 1
  • 5
  • 5

9 Answers9

156

Using pip :

pip install django-cors-headers

Using pipenv :

pipenv install django-cors-headers
LuFFy
  • 8,799
  • 10
  • 41
  • 59
Rafael Corzo
  • 1,851
  • 2
  • 13
  • 21
  • 2
    for Python 2.*, it can be: **pip2 install django-cors-headers** depending on you os+python setup – kmonsoor Apr 20 '15 at 14:09
  • I meet the same problem too. I have tried Undo's answer and it was fixed. But it's strange, I just copy my whole project disk data from an EC2 instance to another EC2 instance, and re-launch it. Then the error occurs. Maybe the new machine's environment is different with the original one. maybe..... – firestoke Apr 21 '15 at 04:43
  • 1
    Anyone who still facing issue with **django-cors-headers** please try again by adding **--user** at the end ```pip install django-cors-headers --user``` – Abhijith Jan 05 '20 at 07:49
7

Try This.

pip install --user django-cors-headers
CPMaurya
  • 371
  • 3
  • 9
7

Step 1. Install corsheaders:

python -m pip install django-cors-headers

Step 2. Add cors-headers to INSTALLED_APPS:

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

Step 3. Add the Cors headers middleware:

MIDDLEWARE = [
    ...,
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    ...,
]

NOTE: Bear in mind that you should place the CorsMiddleware as high as possible to not be overwriten by other middleware or any behaviour caused by other middleware.

Step 4. Add cors headers in settings.py:

CORS_ALLOWED_ORIGINS = [
    'http://127.0.0.1:3000',
    'http://localhost:3030',
    'yoursite.com'
]

OR

CORS_ORIGIN_WHITELIST = [
    'http://127.0.0.1:3000',
    'http://localhost:4200',
    'yoursite.com'
]

Note: Also check whether the endpoint you are requesting are within the CORS_ALLOWED_ORIGINS. Also, avoid using '*' within the same variable to avoid any security breach.

Please, check its documentation there are way more detail and links that can solve some questions you have:

https://github.com/adamchainz/django-cors-headers#setup

Community
  • 1
  • 1
Elias Prado
  • 1,518
  • 2
  • 17
  • 32
1

I had the same problem after I installed via pip. Then I downloaded source and manually installed the django-cors-headers after that the problem was gone.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
1

Step 1. Install corsheaders:

python -m pip install django-cors-headers

Step 2. Add cors-headers to INSTALLED_APPS:

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

Step 3. Add the Cors headers middleware:

MIDDLEWARE = [
    ...,
    'corsheaders.middleware.CorsMiddleware',
    ...,
]

Step 4. Add CORS_ORIGIN_WHITELIST array in settings.py:

CORS_ORIGIN_WHITELIST = [
    'http://127.0.0.1:3000',
    'http://localhost:4200',
    'http://yoursite.com'
]
Mbote-Joseph
  • 73
  • 1
  • 4
0

You probably have a typo in your settings.

INSTALLED_APPS = [
    "corsheaders"
    
]
0

For anyone following Elias Prado's answer with no success, try running your django server on a different port. For me, port 8000 was giving me issues. Probably because that's where I run my work project. Trying port 8008 did the trick!

python3 manage.py runserver 8008

Hope this helps!

-3

From your traceback, it looks like you are not running the Django app in virtual environment. You could do two things:

  1. Install django-corsheaders system wide so it's available to your app, with or without virtual environment

  2. Or you activate the virtual environment and run the django app (since you have already confirmed that django-corsheaders is installed in virtual environment.)

avi
  • 9,292
  • 11
  • 47
  • 84
-3

You don't need to install anything, just use runserver:

python manage.py runserver
J. M. Arnold
  • 6,261
  • 3
  • 20
  • 38
souad988
  • 1
  • 2