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

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.

Leave a Comment