Last modified: Mar 10, 2025 By Alexander Williams
Fix 'No Module Named rest_framework' in Django
If you're working with Django and encounter the error 'No module named rest_framework', don't worry. This is a common issue that can be resolved quickly.
This error usually occurs when Django Rest Framework (DRF) is not installed in your environment. Let's walk through the steps to fix it.
Table Of Contents
Step 1: Install Django Rest Framework
The first step is to ensure that Django Rest Framework is installed. You can install it using pip, Python's package manager.
pip install djangorestframework
After running this command, DRF will be installed in your environment. If you need a detailed guide, check out our article on How to Install Django Rest Framework Step by Step.
Step 2: Add DRF to Installed Apps
Once DRF is installed, you need to add it to your Django project's INSTALLED_APPS
setting. Open your settings.py
file and add 'rest_framework'
to the list.
INSTALLED_APPS = [
...
'rest_framework',
...
]
This step ensures that Django recognizes DRF as part of your project.
Step 3: Verify the Installation
After adding DRF to INSTALLED_APPS
, restart your Django development server. Run the following command:
python manage.py runserver
If the server starts without errors, DRF is correctly installed. If you still see the error, double-check the previous steps.
Step 4: Check Virtual Environment
If you're using a virtual environment, ensure that it is activated. Sometimes, the error occurs because DRF is installed globally, not in the virtual environment.
source venv/bin/activate # On macOS/Linux
venv\Scripts\activate # On Windows
After activating the environment, reinstall DRF using pip.
Step 5: Reinstall Django Rest Framework
If the issue persists, try reinstalling DRF. Uninstall it first, then install it again.
pip uninstall djangorestframework
pip install djangorestframework
This can resolve any installation issues that might be causing the error.
Step 6: Check Python Path
Ensure that your Python path is correctly set. Sometimes, the error occurs because Python cannot find the installed package.
You can check your Python path by running:
python -m site
This command will display the directories Python searches for packages. Ensure that your virtual environment's site-packages directory is included.
Step 7: Use DRF in Your Views
Once DRF is installed and configured, you can start using it in your Django views. For example, you can create an API view using APIView
.
from rest_framework.views import APIView
from rest_framework.response import Response
class HelloWorldView(APIView):
def get(self, request):
return Response({"message": "Hello, World!"})
For more examples, check out our article on Django Rest Framework's APIView: Examples and Usage.
Conclusion
Fixing the 'No module named rest_framework' error in Django is straightforward. Ensure DRF is installed, added to INSTALLED_APPS
, and your environment is correctly configured.
By following these steps, you can resolve the error and start building powerful APIs with Django Rest Framework. If you need more advanced examples, explore our guide on Django Rest Framework ListCreateAPIView Example.