ModuleNotFoundError: No module named ‘rest_framework’ [Solved]

When you are working with rest_frame, you might face ModuleNotFoundError: No module named ‘rest_framework’ error which occurs when you forget to install djangorestframework module on your system or when the script fails to import it. In this short article, we will learn how we can install the djangorestframework on our system using various methods to that we will not get ModuleNotFoundError: No module named ‘rest_framework’ error anymore. Moreover, we will also discuss how to understand errors in Python and solve them.

ModuleNotFoundError: No module named ‘rest_framework’

The ModuleNotFoundError: No module named ‘rest_framework’ error occurs when you tried to run the script using djangorestframework without installing it on your system.

ModuleNotFoundError: No module named 'rest_framework-error

Here are various methods to solve the issue and install the required module on your system.

Solution-1: Install module using pip command

If you are using the pip command to install the modules on your system, then you can use the following commands to get rid of the problem.

#  using Python 2
pip install djangorestframework

# for pip3 version
pip3 install djangorestframework

# using --user permission
pip install djangorestframework --user

If you are using Linux, then you can use the following commands:

# linux users
sudo pip3 install djangorestframework

Here are other alternative methods to solve the same error:

#PATH environment variable
python -m pip install djangorestframework

#  for python 3 
python3 -m pip install djangorestframework

# using py alias 
py -m pip install djangorestframework

#  for Anaconda
conda install -c conda-forge djangorestframework

#  Jupyter Notebook
!pip install djangorestframework

Hopefully, the above-mentioned methods will help you to install djangorestframework so that you will not get ModuleNotFoundError: No module named ‘rest_framework’ error anymore.

Solution-2: Adding djangorestframework

Once the installation of django rest framework is done, you need to add the rest_framework to your installed apps:

INSTALLED_APPS = [
    # ...
    'rest_framework',
]

Make sure, you have added a comma after rest_framework because if then you will again get ModuleNotFoundError: No module named ‘rest_framework’ error.

Now you can import the rest_framwork using the following commands:

from rest_framework import routers, serializers, viewsets

Now you will no more get the error:

Solution-3: Alternative solution

Another alternative solution is to create a virtual environment and then install the required module on a virtual environment.

You can use the following commands to create and activate the virtual environment on your system.

# using python3 
python3 -m venv venv

#activate on Unix or MacOS
source venv/bin/activate

# activate on Windows (cmd.exe)
venv\Scripts\activate.bat

# activate on Windows (PowerShell)
venv\Scripts\Activate.ps1

Once you have activated the virtual environment, the next step is to install the module. You can use any of the given methods to install the module in a virtual environment:

#  using Python 2
pip install djangorestframework

# for pip3 version
pip3 install djangorestframework

# using --user permission
pip install djangorestframework --user

# linux users
sudo pip3 install djangorestframework

#PATH environment variable
python -m pip install djangorestframework

#  for python 3 
python3 -m pip install djangorestframework

# using py alias 
py -m pip install djangorestframework

#  for Anaconda
conda install -c conda-forge djangorestframework

#  Jupyter Notebook
!pip install djangorestframework

Now, you can use the module without getting the error:

Solution-4: Uninstalling the module

If you are still getting ModuleNotFoundError: No module named ‘rest_framework’ error then you can try to uninstall the module and then install it again on your system.

You can use any of the following commands to uninstall the module, depending on your environment:

#  check if you have djangorestframework installed
pip3 show djangorestframework

# you don't have pip set up in PATH
python3 -m pip show djangorestframework

# uninstall djangorestframework
pip3 uninstall djangorestframework

#  if you don't have pip set up in PATH
python3 -m pip uninstall djangorestframework

Once you have uninstalled the module, you can again use any of the following methods to install it again on your system.

#  using Python 2
pip install djangorestframework

# for pip3 version
pip3 install djangorestframework

# using --user permission
pip install djangorestframework --user

# linux users
sudo pip3 install djangorestframework

#PATH environment variable
python -m pip install djangorestframework

#  for python 3 
python3 -m pip install djangorestframework

# using py alias 
py -m pip install djangorestframework

#  for Anaconda
conda install -c conda-forge djangorestframework

#  Jupyter Notebook
!pip install djangorestframework

Hopefully, now you will no more get the error.

Solution-5: Upgrading the module

Another method to get rid of the error instead of uninstalling and installing again, you can simply to upgrade the existing djangorestframework on your system using the following commands:

# using pip 3 verison
pip3 install djangorestframework --upgrade

# if you don't have pip set up in PATH
python3 -m pip install djangorestframework --upgrade

Hopefully, you will no more get ModuleNotFoundError: No module named ‘rest_framework’ error.

Understanding the ModuleNotFoundError: No module named ‘rest_framework’ error

In Python usually, the errors give much more information. It has two parts. The first part gives information about the category of the error which in this case is the ModuleNotFound. The second part of the error gives more specific information and gives us details about the module that was not correctly imported.

ModuleNotFoundError: No module named 'rest_framework'

What is the ModuleNotFound error in Python?

When a module that has been imported into a program cannot be found, the Python interpreter throws the ModuleNotFoundError error. It typically appears when a module’s name is incorrect or when the module is not in the interpreter’s search route. You must make sure the module name is spelled correctly and that it is located in a directory that is included in the interpreter’s search path in order to correct the mistake. To import the module dynamically, you may alternatively try using the importlib.import module() function.

Reasons for getting ModuleNotFoundError: No module named ‘rest_framework’

The following are some of the main reasons for getting ModuleNotFoundError: No module named ‘rest_framework’ error:

  • Not having Django rest framework installed on the system.
  • Installed package using different versions of Python
  • Installed package globally and not in a virtual environment
  • Maybe your IDE running an error
  • Maybe there is a typo mistake

Summary

In this short article, we discussed how we can solve ModuleNotFoundError: No module named ‘rest_framework’ error using various methods. We discussed five different methods to solve the error. Moreover, we also covered some of the solid reasons for getting ModuleNotFoundError: No module named ‘rest_framework’ error.

Related issues:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top