[Django]-Django Rest Framework โ€” no module named rest_framework

215๐Ÿ‘

โœ…

You need to install django rest framework using pip3 (pip for python 3):

pip3 install djangorestframework

Instructions on how to install pip3 can be found here

๐Ÿ‘คVingtoft

78๐Ÿ‘

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',
]
๐Ÿ‘คrpstw

23๐Ÿ‘

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โ€ฆ ๐Ÿ˜…

๐Ÿ‘คmartin-martin

8๐Ÿ‘

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.

๐Ÿ‘คO. Edholm

6๐Ÿ‘

if after installing and adding it to your INSTALLED_APPS it persist, then itโ€™s most likely because youโ€™re using python3 to run the server and thats okay. So what you do while installing is use python3 -m pip install djangorestframework .

๐Ÿ‘คHenry2

4๐Ÿ‘

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.

๐Ÿ‘คBurger King

3๐Ÿ‘

The command which worked for me is

python -m pip install djangorestframework
๐Ÿ‘คPratik Patil

2๐Ÿ‘

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.

๐Ÿ‘คOleg G

2๐Ÿ‘

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.

๐Ÿ‘คAnna M.

2๐Ÿ‘

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

๐Ÿ‘คvillageek

2๐Ÿ‘

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

๐Ÿ‘คUsama kaleem

2๐Ÿ‘

Also, if youโ€™re getting this error while running docker-compose up. Make sure to run docker-compose up --build because docker needs to install the djangorestframework dependency as well.

๐Ÿ‘คErnestoM

2๐Ÿ‘

I have faced the same issue and And solve it with upgrading the pip and install rest_framework after that.(update everything)

Windows Command Prompt

> python -m pip install --upgrade pip

Linux Terminal

$ pip install --upgrade pip

MacOS Terminal

$ pip install --upgrade pip

Install Django and Django REST framework

pip install django
pip install djangorestframework
๐Ÿ‘คsajjad

1๐Ÿ‘

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)

๐Ÿ‘คggcarmi

1๐Ÿ‘

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

machine@debian:/$ sudo pip install djangorestframework
or;
root@debian:/# pip install djangorestframework
๐Ÿ‘ค7guyo

1๐Ÿ‘

(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).

๐Ÿ‘คgmagno

1๐Ÿ‘

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',
    ]
    

1๐Ÿ‘

Install the missing module separately using pip installer

pip3 install djangorestframework-jsonapi

This worked for me.

๐Ÿ‘คSridhar Murali

1๐Ÿ‘

After installing the necessary packages with python3/pip3 inside my virtual environment, it all came down to running my server with python manage.py runserver instead of python3 manage.py runserver. This was because the virtual environment and other packages were installed using python3/pip3 and not python2/pip2, hence running the server with python3 again resulted in the error. Am sure this will help someone else.

๐Ÿ‘คtheBelovedKing

0๐Ÿ‘

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

๐Ÿ‘คAkshay Kumbhar

0๐Ÿ‘

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.

๐Ÿ‘คDevSR

0๐Ÿ‘

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

๐Ÿ‘คPrateek Gupta

0๐Ÿ‘

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.

๐Ÿ‘คJosh

0๐Ÿ‘

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.

๐Ÿ‘คcoderina

0๐Ÿ‘

I face the same problem. In my case, I solved it by update Windows Defender configuration.

๐Ÿ‘คBinti Solihah

0๐Ÿ‘

activate the whole virtual environment

cd django_apps
source /root/django-apps/env/bin/activate
๐Ÿ‘คYidnek

0๐Ÿ‘

Install pip3 install djangorestframework first
and add rest_framework in the settings.py.
This is how I have a shout out the problem.

๐Ÿ‘คnawaraj1111

0๐Ÿ‘

Make sure you are using the same language interpreter which you have used in your Django project, which can be an interpreter in the virtual environment, or like me, I have a normal python installed and an anaconda python too. So, try switching the interpreter.
See this image for reference

๐Ÿ‘คBhanu Pratap

-1๐Ÿ‘

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

๐Ÿ‘คGizmoa

-1๐Ÿ‘

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

๐Ÿ‘คVijayGupta

-2๐Ÿ‘

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.

๐Ÿ‘คVikhyath Rai

Leave a comment