Modulenotfounderror: no module named corsheaders

In this article, we will learn the solutions on how to fix the no module named corsheaders. The solution to this error is to reinstall the django-cors-headers module.

If we reinstall any Python module, we first uninstall the older version and then install the latest or the given version.

This naturally resolves all the incompatibility. Eventually, we must be careful when upgrading or downgrading while reinstalling the package module.

The Python “ModuleNotFoundError: No module named ‘corsheaders’ ” error will appear.

If we forget to install the corsheaders module before importing it or install it in the wrong environment.

To fix this error, we will install the module by running the following command.

pip install django-cors-headers

If you type the above command it will show you the result like this:

Solutions to Solve the no module named corsheaders

Install the django-cors-headers module by opening your terminal from the project’s root directory.

Time needed: 3 minutes

Here are the various solutions to solve the Modulenotfounderror: no module named corsheaders

  • Solution 1: django-cors-headers installation using pip

    As we discussed this is the best and simple way to install any python package. This is the following command for installing in Python 2 virtual environment:
    pip install django-cors-headers

  • Solution 2: django-cors-headers installation in Python 3

    This is the following command for installing in Python 3 virtual environment:
    pip3 install django-cors-headers

  • Solution 3: django-cors-headers get permissions error

    This is the following command for installing if you get permissions error:
    sudo pip3 install django-cors-headers
    or
    pip install django-cors-headers –user

  • Solution 4: No install PIP in the Path

    If you did not install the pip in your PATH environment variable. Type this command to install the correct path of pip:
    python -m pip install django-cors-headers

  • Solution 4: No install PIP in the Path for Python 3

    Type this command to install the correct path of pip in Python 3:
    python3 -m pip install django-cors-headers

  • Solution 5: using py alias (Windows)

    Type this command to install using py alias(windows):
    py -m pip install django-cors-headers

  • Solution 6: django-cors-headers installation in Anaconda

    Type this command for the installation in Anaconda:
    conda install -c conda-forge django-cors-headers

  • Solution 7: django-cors-headers installation in Jupyter Notebook

    Type this command for the installation in Anaconda:
    !pip install django-cors-headers

If you have already installed django-cors-headers module, add it to your installed apps:

In your settings.py

INSTALLED_APPS = [
#
    "corsheaders",
#
]

Make sure to add a comma at the end for not causing an error problem like “ModuleNotFoundError“.

Also, add this to your middleware class in settings.py:

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

Note: The “CorsMiddleware” must be placed as high as possible, before any middleware that can set up responses, like the “CommonMiddleware” and “WhiteNoiseMiddleware”.

When the error will continue, check your Python version and make sure you are installing the package module by using the correct version of Python.

Type this command to show the version of your python:

python --version
Modulenotfounderror: no module named corsheaders python version

Check if the module package is installed

You can check if your django-cors-headers is installed on your computer by typing this command:

pip show django-cors-headers

After you type this command: pip show django-cors-headers whether the package is not installed or it will show an information about the package, it will include the location where the package is installed.

Errors Occurs in Multiple Reasons:

  • Package Module “django-cors-headers” is not installed on your system
  • You may install the package in a different Python version
  • You may install it globally but not in your virtual environment
  • You may run the IDE in an incorrect version of python

Frequently Asked Questions

What is Python ModuleNotFoundError and what causes it?

ModuleNotFoundError (a subclass of ImportError) is raised when Python cannot find the module you tried to import. Common causes: the package isn’t installed (pip install missing), wrong virtual environment activated, typo in module name, or Python can’t find your local module on the import path. The error message names exactly which module is missing.

How do I fix ‘ModuleNotFoundError: No module named X’?

Run pip install X first. If that succeeds but you still get the error, check which Python you’re using (which python OR python –version) vs which pip (which pip OR pip –version), they must match. Common gotcha: pip points to system Python 3.9 but you’re running python3.11 in a venv. Inside the venv, use python -m pip install X to be sure pip matches the active Python.

Why does my code work in one environment but not another?

Different Python versions or different installed packages. To diagnose: pip freeze > requirements.txt on the working environment, then pip install -r requirements.txt on the broken one. Use virtualenv (python -m venv venv) or conda for every project to avoid system-wide package collisions.

Is ModuleNotFoundError the same as ImportError?

ModuleNotFoundError is a subclass of ImportError added in Python 3.6. It specifically means ‘no such module exists.’ Plain ImportError covers a wider set: module exists but a name inside it can’t be imported (e.g. ‘cannot import name X from Y’). except ImportError catches both; except ModuleNotFoundError catches only the missing-module case.

Where can I find more ModuleNotFoundError fixes?

Browse the ModuleNotFoundError reference hub for 198+ specific module fixes (TensorFlow, Flask, Django, pandas, numpy, etc.). For related issues see ImportError. For broader Python setup see Python Tutorial hub.

Conclusion

To conclude in this article, we provided the solutions to solve the error Modulenotfounderror: no module named corsheaders in windows, anaconda and Jupyter Notebook.

Adones Evangelista

Programmer & Technical Writer at PIES IT Solution

Adones Evangelista is a programmer and writer at PIES IT Solution, author of over 900 tutorials and error-fix guides at itsourcecode.com. Specializes in JavaScript, Django, Laravel, and Python error debugging covering ValueError, TypeError, AttributeError, ModuleNotFoundError, and RuntimeError, plus C/C++ and PHP capstone projects for BSIT students.

Expertise: JavaScript · Python · Django · Laravel · Error Debugging · C/C++  · View all posts by Adones Evangelista →

Leave a Comment