import error django corsheaders
PythonDjangoAmazon Ec2Django Cors-HeadersPython Problem Overview
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
Python Solutions
Solution 1 - Python
Using pip
:
pip install django-cors-headers
Using pipenv
:
pipenv install django-cors-headers
Solution 2 - Python
Try This.
pip install --user django-cors-headers
Solution 3 - Python
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.
Solution 4 - Python
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'
]
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:
Solution 5 - Python
You don't need to install anything, just use runserver
:
python manage.py runserver
Solution 6 - Python
You probably have a typo in your settings.
INSTALLED_APPS = [
"corsheaders"
]
Solution 7 - Python
From your traceback, it looks like you are not running the Django app in virtual environment. You could do two things:
-
Install
django-corsheaders
system wide so it's available to your app, with or without virtual environment -
Or you activate the virtual environment and run the django app (since you have already confirmed that
django-corsheaders
is installed in virtual environment.)