[python] Django Rest Framework -- no module named rest_framework

I've installed django rest framework using pip install djangorestframework yet I still get this error when I run "python3 manage.py sycndb":

ImportError: No module named 'rest_framework'

I'm using python3, is this my issue?

This question is related to python django python-3.x pip django-rest-framework

The answer is


(I would assume that folks using containers know what they're doing, but here's my two cents)

Let's say you setup your project using cookiecutter-django and enabled the docker container support, be sure to update the pip requirements file with djangorestframework==<x.yy.z> (or whichever python dependency you're trying to install) and re-build the docker images (local and production).


In my case, I had installed it in the virtualenv but forgot to activate the virtualenv while running the command

 python3 manage.py makemigrations 

So in my case I had to just activate the environment and then run the command

source [virtualenv folder-name]/bin/activate
python3 manage.py makemigrations

This solved my problem.


Also, check for the possibility of a tiny typo:

It's rest_framework with an underscore (_) in between!

Took me a while to figure out that I was using a dash instead...


If you're using some sort of virtual environment do this!

  1. Exit from your virtual environment.

  2. Activate your virtual environment.

After you've done this you can try running your command again and this time it probably won't have any ImportErrors.


try this if you are using JWT pip install djangorestframework-jwt


On Windows, with PowerShell, I had to close and reopen the console and then reactive the virtual environment.


When using a virtual environment like virtualenvwithout having django-rest-framework installed globally you might as well have the error. The solution would be:

  • activate the environment first with {{your environment name}}/bin/activate for Linux or {{your environment name}}/Scripts/activate for Windows

  • and then run the command again.


If you are working with PyCharm, I found that restarting the program and closing all prompts after adding 'rest_framework' to my INSTALLED_APPS worked for me.


First installing the framework globally on the system solved my problem.

machine@debian:/$ sudo pip install djangorestframework
or;
root@debian:/# pip install djangorestframework

INSTALLED_APPS = [
'rest_framework',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#apps
'apps.endpoints',

]

maybe forgot the comma "," or while pasting packing name it might have extra whitespace "packagename "check for that


In my case, my problem was different. I was creating in my bash_profile an alias like:

alias python=/usr/local/bin/python3

And even if I activate my environment, when I ran the command, the python interpreter accessed was from the system and not from my environment.

I just removed the alias from bash_profile and it worked fine.


Yeh for me it was the python version as well ...
much better to use pipenv ...
create a virtual env using using python 3 ...

install pipenv : pip3 install pipenv
create the virtualenv: pipenv --python 3
activate the virtual env: pipenv shell


I know there is an accepted answer for this question and many other answers also but I just wanted to add an another case which happened with me was Updating the django and django rest framework to the latest versions to make them work properly without any error.

So all you have to do is just uninstall both django and django rest framework using:

pip uninstall django pip uninstall djangorestframework

and then install it again using:

pip install django pip install djangorestframework


Install the missing module separately using pip installer

pip3 install djangorestframework-jsonapi

This worked for me.


I recently installed the latest Django 3.1 and Django Rest Framework 3.11.1 libraries only to eventually realize Django 3.1 is not supported by DRF as of 11 April 2020. I did not realize that the exact releases mentioned need to be used.

If you're pulling your hair out because you can't understand why DRF is not importing check these requirements and make sure your app is compatible.


I've faced the same problem, followed these instructions and it worked for me:

  1. python -m pip install --upgrade pip (to upgrade pip)
  2. pip3 install djangorestframework
  3. Added rest_framework as first app:

    INSTALLED_APPS = [
        'rest_framework',
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        #apps
        'apps.endpoints',
    ]
    

rest_framework module

Try pressing the icon given if you are getting the following error:

"ModuleNotFoundError: No module named 'rest_framework'"

It will ask you to import the given package, that is rest_framework and run the code again.

This worked for me and trying to solve that error for a couple of days.


if you used pipenv:

if you installed rest_framework thru the new pipenv, you need to run it thru the virtual environment:

1.pipenv shell

2.(env) now, run your command(for example python manage.py runserver)


Maybe you install DRF is for python2, not for python3.

You can use python console to check your module:

import rest_framework

Actually you use pip to install module, it will install python2 module.

You should install the pip for python3:

sudo apt-get install python3-setuptools
sudo easy_install3 pip

So, you can install python3 module.


if you forget ,,this will happen,it's weird

wrong example: need a ,

INSTALLED_APPS = [
'rest_framework'
'django.contrib.contenttypes',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

To install it, do the following:

pip install djangorestframework
pip install markdown     
pip install django-filter

We have to check for a few common mistakes:

  1. check comma at installed list elements

  2. typo errors


Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to django

How to fix error "ERROR: Command errored out with exit status 1: python." when trying to install django-heroku using pip Pylint "unresolved import" error in Visual Studio Code Is it better to use path() or url() in urls.py for django 2.0? Unable to import path from django.urls Error loading MySQLdb Module 'Did you install mysqlclient or MySQL-python?' ImportError: Couldn't import Django Django - Reverse for '' not found. '' is not a valid view function or pattern name Class has no objects member Getting TypeError: __init__() missing 1 required positional argument: 'on_delete' when trying to add parent table after child table with entries How to switch Python versions in Terminal?

Examples related to python-3.x

Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation Replace specific text with a redacted version using Python Upgrade to python 3.8 using conda "Permission Denied" trying to run Python on Windows 10 Python: 'ModuleNotFoundError' when trying to import module from imported package What is the meaning of "Failed building wheel for X" in pip install? How to downgrade python from 3.7 to 3.6 I can't install pyaudio on Windows? How to solve "error: Microsoft Visual C++ 14.0 is required."? Iterating over arrays in Python 3 How to upgrade Python version to 3.7?

Examples related to pip

How to fix error "ERROR: Command errored out with exit status 1: python." when trying to install django-heroku using pip "E: Unable to locate package python-pip" on Ubuntu 18.04 How to Install pip for python 3.7 on Ubuntu 18? What is the meaning of "Failed building wheel for X" in pip install? Could not install packages due to an EnvironmentError: [Errno 13] How do I install Python packages in Google's Colab? Conda version pip install -r requirements.txt --target ./lib pip: no module named _internal AttributeError: Module Pip has no attribute 'main' Error after upgrading pip: cannot import name 'main'

Examples related to django-rest-framework

"Post Image data using POSTMAN" How can I enable CORS on Django REST Framework Django Rest Framework -- no module named rest_framework OperationalError, no such column. Django Django Rest Framework File Upload Django REST Framework: adding additional field to ModelSerializer Retrieving a Foreign Key value with django-rest-framework serializers