Django Rest Framework -- no module named rest_framework
Asked Answered
T

32

129

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?

Typhon answered 23/10, 2015 at 18:5 Comment(6)
Have you installed in settings.py?Communism
@BurgerKing I added rest_framework to my installed apps. Is that what you meant?Typhon
Are you using a virtuel environment?Sorensen
I just try to guess reason. May you give your error message?Communism
@Sorensen I'm not. I was following this tutorial: youtube.com/watch?v=VZ8NIoLN-yQ at the 5 minute mark.Typhon
@BurgerKing "ImportError: No module named 'rest_framework'" is the error.Typhon
S
225

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

Sorensen answered 23/10, 2015 at 18:18 Comment(7)
I am not sure why but I am still facing the same issueCurdle
This solves my problem. In my case I have done codepip install djangorestframeworkcode in the windows powershellJetliner
if pip3 does not work, try: python3 -m pip install djangorestframeworkHimation
Instead of answers that point out the obvious ("install django, duh") check that you don't have a missing comma in the INSTALLED_APPS list, because that weirdly triggers this ImportError. Kudos to this answer.Trophoplasm
@alexlomba87, this error also appears if you use python3 but have installed django using pip for python 2.7, which is why this answer does not point out the obvious for many developers.Sorensen
Thank you for adding that, I'd advice to update the answer to add that info, which is not explicit. Pip3 may be the default for many.Trophoplasm
/usr/bin/env /bin/python3 manage.py makemigrations fixed my problemMaracanda
E
80

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',
]
Explanatory answered 11/7, 2017 at 11:56 Comment(3)
Glad I scrolled down past trying to reinstall DRF. This solved it for me.Polarize
Such a silly thing xD. It helped me thanksArabele
I also made this silly mistake. Your solution worked in this case. Thank you!Used
E
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... 😅

Engraft answered 27/9, 2018 at 20:34 Comment(3)
I agree such a sneaky thing.Deoxygenate
Thanks for mentioning typo. I am using rest_framwork instead of rest_framework. Missing e in framework. :)Taxation
And here I am trying to find what sort of dark arts I needed to perform to make this work, and all it was, was a damn typo! Thank you!Doting
A
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.

Amadeus answered 6/6, 2018 at 21:36 Comment(1)
to active your virtual environment, please use source venv/bin/activate or source env/bin/activate depends on your folder.Wrinkle
U
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 .

Uppercut answered 24/2, 2022 at 13:51 Comment(1)
Don't know why python -m as prefix works in virtual env for me. Thanks.Granitite
C
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.

Communism answered 23/10, 2015 at 18:18 Comment(1)
I see what you mean. I'm getting an error: "apt-get: command not found" when I try to run your command though.Typhon
K
3

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.

Kratz answered 19/7, 2016 at 23:6 Comment(1)
THANK YOU for reminding me! I forgot to activate the VENV for this console! ARG! (Feeling pretty silly right about now for sure.) :facepalm:Ringe
G
3

The command which worked for me is

python -m pip install djangorestframework
Gus answered 25/8, 2022 at 10:30 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewSmashed
Z
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.

Zygophyllaceous answered 24/9, 2018 at 7:4 Comment(0)
T
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

Tel answered 23/4, 2020 at 8:26 Comment(0)
A
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

Allomerism answered 20/10, 2020 at 6:7 Comment(0)
C
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.

Chesser answered 1/6, 2021 at 17:17 Comment(0)
F
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
February answered 9/4, 2022 at 7:53 Comment(0)
G
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)

Gerenuk answered 16/1, 2019 at 8:31 Comment(0)
C
1

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

machine@debian:/$ sudo pip install djangorestframework
or;
root@debian:/# pip install djangorestframework
Centurial answered 23/4, 2019 at 8:18 Comment(0)
S
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).

Superannuate answered 7/12, 2019 at 21:27 Comment(1)
Good catch. This was my case. Forgot to re-run pip freeze to update my requirements.txt file which is what the Dockerfile has as dependencies that get installed into the api container.Oleary
R
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',
    ]
    
Rosalie answered 21/3, 2020 at 18:16 Comment(1)
why is it need put first app tell me please?Landonlandor
T
1

Install the missing module separately using pip installer

pip3 install djangorestframework-jsonapi

This worked for me.

Teteak answered 24/2, 2021 at 7:57 Comment(0)
N
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.

Nigelniger answered 6/7, 2021 at 21:43 Comment(0)
G
0

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

Garth answered 24/3, 2017 at 16:2 Comment(0)
H
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.

Hodges answered 24/10, 2019 at 21:1 Comment(1)
Nice work on your first answer, but make sure you format code correctly. stackoverflow.com/editing-help.Pistachio
H
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

Hetaerism answered 17/4, 2020 at 20:28 Comment(0)
A
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.

Abelabelard answered 11/8, 2020 at 13:51 Comment(0)
E
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.

Enduring answered 3/11, 2020 at 8:39 Comment(0)
L
0

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

Lashonda answered 12/6, 2021 at 0:20 Comment(0)
P
0

activate the whole virtual environment

cd django_apps
source /root/django-apps/env/bin/activate
Pellet answered 24/6, 2021 at 15:8 Comment(0)
C
0

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

Cyanate answered 2/8, 2021 at 18:46 Comment(0)
R
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

Reorganization answered 20/6, 2022 at 21:19 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Scarlatina
D
0

The best solution for MAC or LINUX as I tried:

  1. Open your project path ( where the settings.py is located )
  2. Copy the path from the explorer
  3. Open your terminal
  4. Type cd[space][paste your path location that you copied before]

then try this:

pip install djangorestframework

it's gonna solve.

REMEMBER: ON YOUR TERMINAL OF MAC OS OR LINUX OS NOT VsCode

point: it also worked for all the other libraries like: cors-headers

Dicky answered 17/1 at 11:4 Comment(0)
A
-1

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

Almetaalmighty answered 7/6, 2019 at 6:51 Comment(0)
E
-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

Ellerd answered 28/2, 2020 at 15:20 Comment(0)
H
-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.

Highness answered 28/5, 2018 at 10:35 Comment(1)
Not everyone uses the same editor, and not all editors provide the same feature to import modules through the light bulb.Sporadic

© 2022 - 2024 — McMap. All rights reserved.