Django cannot import corsheaders (django-cors-headers)
ModuleNotFoundError: No module named 'corsheaders'Short answer
The pip package is django-cors-headers, but the module Django imports is corsheaders. The error means the Python that runs Django cannot find that module: it was installed in another environment, you installed a package with the wrong name, or INSTALLED_APPS is missing a comma.
Fix it fast
1Find out which cause you have
Ask the exact Python that runs your site whether it can see the package. Use the interpreter from your service or virtual environment, not whichever python is first on your path.
/path/to/venv/bin/python -m pip show django-cors-headers /path/to/venv/bin/python -c "import corsheaders; print(corsheaders.__file__)"2Match what you see, then jump to its fix
- 1
pip install succeeds, yet the web server still fails (common on a server with several Python installs)The package is installed in a different Python environmentFix - 2
pip show django-cors-headers prints nothingThe wrong package name was installedFix - 3
The package is installed and imports in a shell, but Django still reports the missing moduleINSTALLED_APPS lacks a comma after the entryFix
- 1
What does the error look like?
ModuleNotFoundError: No module named 'corsheaders'ImportError: No module named corsheaders
Cause 1 of 3
The package is installed in a different Python environment
1Check
Compare the interpreter your service uses with the one pip belongs to.
which python pip
systemctl cat gunicorn | grep -i ExecStart2Fix
Install with the same interpreter that runs Django. Using python -m pip guarantees that pairing.
/path/to/venv/bin/python -m pip install django-cors-headers3Verify
Restart the service and import the module with that interpreter.
sudo systemctl restart gunicorn
/path/to/venv/bin/python -c "import corsheaders"Confirmed by github.com
Cause 2 of 3
The wrong package name was installed
1Check
The project's own page states that the pip package is django-cors-headers, while the module is corsheaders. Installing a package literally named corsheaders is a different thing.
python -m pip list | grep -i cors2Fix
Install the right package and use corsheaders in settings.
python -m pip install django-cors-headers3Verify
Import the module.
python -c "import corsheaders; print('ok')"Confirmed by github.com
Cause 3 of 3
INSTALLED_APPS lacks a comma after the entry
1Check
The project's own page warns that leaving out the trailing comma after corsheaders can raise ModuleNotFoundError. Look at the neighbouring entries.
grep -n -B2 -A2 corsheaders settings.py2Fix
Add the app with its trailing comma, and put the middleware before CommonMiddleware.
INSTALLED_APPS = [
# ...
"corsheaders",
]
MIDDLEWARE = [
"corsheaders.middleware.CorsMiddleware",
"django.middleware.common.CommonMiddleware",
# ...
]3Verify
Run Django's system check.
python manage.py checkConfirmed by github.com
How do I stop it coming back?
- Keep dependencies in requirements.txt and install them with the same interpreter that serves the site.
- Install with python -m pip, never a bare pip, on servers with more than one Python.
- After adding a package, restart the app server, which loads modules only at start.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Airflow SSHOperator stops a long script with SSH command timed out
airflow.exceptions.AirflowException: SSH command timed out
SSHOperator waits only 10 seconds for a command by default.
3 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ASP.NET Core has CORS set up but the browser still blocks the request
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
ASP.NET Core only adds CORS headers when a policy matches the request.
6 causes, each with a fixBack end
Python asyncio: Task was destroyed but it is pending
RuntimeWarning: coroutine '...' was never awaited
A task was still unfinished when Python threw it away.
4 causes, each with a fixBack end
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
bcrypt.hash throws 'data and salt arguments required'
Error: data and hash arguments required
The bcrypt package throws this when hash gets null or undefined for the password or for the salt.
4 causes, each with a fix