Import Error Django Corsheaders
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...
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
Using pip :
pip install django-cors-headers
Using pipenv :
pipenv install django-cors-headers
Try This.
pip install --user django-cors-headers
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 = [
'
'
'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:
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.
You don't need to install anything, just use runserver:
python manage.py runserver
You probably have a typo in your settings.
INSTALLED_APPS = [
"corsheaders"
]
From your traceback, it looks like you are not running the Django app in virtual environment. You could do two things:
Install
django-corsheaderssystem wide so it's available to your app, with or without virtual environmentOr you activate the virtual environment and run the django app (since you have already confirmed that
django-corsheadersis installed in virtual environment.)