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?
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
codepip install djangorestframeworkcode in the windows powershellThis happens if you forget to put a , after adding rest_framework to INSTALLED_APPS, like this:
INSTALLED_APPS = [
'rest_framework'
'django.contrib.contenttypes',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
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... 😅
rest_framwork instead of rest_framework. Missing e in framework. :)If you're using some sort of virtual environment do this!
Exit from your virtual environment.
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.
source venv/bin/activate or source env/bin/activate depends on your folder.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 .
The command which worked for me is
python -m pip install djangorestframework
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.
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.
:facepalm:I have faced the same issue and And solve it with upgrading the pip and install rest_framework after that.(update everything)
> python -m pip install --upgrade pip
$ pip install --upgrade pip
$ pip install --upgrade pip
Install Django and Django REST framework
pip install django
pip install djangorestframework
(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).
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
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, 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.
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.
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
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.
I've faced the same problem, followed these instructions and it worked for me:
python -m pip install --upgrade pip (to upgrade pip)pip3 install djangorestframeworkAdded 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',
]
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.
The best solution for MAC or LINUX as I tried:
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
settings.py?