'Import "rest_framework" could not be resolved. But I have installed djangorestframework, I don't know what is going wrong

Here's my settings.py:

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


Solution 1:[1]

If you are using VSCode, Ctrl + Shift + P -> Type and select 'Python: Select Interpreter' and enter into your projects virtual environment. This is what worked for me.

Solution 2:[2]

  1. Run in terminal

    pip install django-rest-framework
    
  2. Add 'rest_framework' to INSTALLED APPS in settings.py

  3. If it does not work, restart the editor(vscode or something)

Solution 3:[3]

  1. Control+ shift + p.
  2. type 'Python: Select Interpreter' and select the same.
  3. choose your virtual env from the list if it is not listed please choose Enter Interpreter path'
  4. Give pathe like this 'c:\users\user\django\myvenv\scripts\python.ex'

Solution 4:[4]

What if you selected the right interpreter (that of virtual environment), but still getting that error?

Then first check the path of your pip in the terminal of that virtual environment.
Use this command: which pip
It should only point to the path where your virtual environment folder is located, for example:

name_of_virtualenv/bin/pip
name_of_virtualenv/Scripts/pip

If shown otherwise, to solve this issue:

  1. delete the virtual environment folder and create it again.
  2. which pip must show the right path now in the new virtual env terminal.
  3. reinstall the packages.

Solution 5:[5]

Add your python file like C:\Users\hendrialqory\AppData\Local\Programs\Python\Python39-32\Lib\site-packages, Go to Setting Environment Variables and input your file python.

Solution 6:[6]

I was having similar problem.

  • go to your venv folder > Lib
  • make sure you see the djangorestframework and rest_framework folder
  • if they are absent, you should know you are pip installing those packages in the wrong venv.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nayburz
Solution 2 Mohnish
Solution 3 Faseela Thayattuchira
Solution 4 Matin Sasan
Solution 5 user16792619
Solution 6 Joooeey