Import "rest_framework" could not be resolved. But I have installed djangorestframework, I don't know what is going wrong
Question:
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'
]
Answers:
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.
-
Run in terminal
pip install django-rest-framework
-
Add 'rest_framework'
to INSTALLED APPS in settings.py
-
If it does not work, restart the editor(vscode or something)
Add your python file like C:UsershendrialqoryAppDataLocalProgramsPythonPython39-32Libsite-packages, Go to Setting Environment Variables and input your file python.
- Control+ shift + p.
- type ‘Python: Select Interpreter‘ and select the same.
- choose your virtual env from the list
if it is not listed please choose
Enter Interpreter path’
- Give pathe like this ‘c:usersuserdjangomyvenvscriptspython.ex’
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:
- delete the virtual environment folder and create it again.
which pip
must show the right path now in the new virtual env terminal.
- reinstall the packages.
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.
Answers of @nayburz and @Faseela worked for me.
- Control+ shift + p.
- type ‘Python: Select Interpreter’ and select the same.
- choose your virtual env from the list if it is not listed please choose Enter Interpreter path’
- path of your virtual env python.exe file.
mine was : D:PythonPython_DjangotrydjangoenvScriptspython.exe
You can find similar path according to your project
for others using venv and if doing ‘Python:Selecting Interpreter’ and selecting your venv doesn’t work then try these steps. These are for Mac.
- open Terminal
- navigate to your project folder.
- activate virtual environment ( > source {path_to_venv_home}binactivate)
- now open vscode with ‘code .’
In case ‘code .’ doesn’t work then should add to your path.
Try ‘Launching from the command Line’ section from https://code.visualstudio.com/docs/setup/mac
If you have set all as mentioned here, and you still have that error. makesure you check in your code editor if you are in the directory where your virtual environement is found.
It is good to mention that, when you open your code editor (VS code for example) makesure your open at the directory where your Virtual environment (venv) is found. this will certainly be the case.
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'
]
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.
-
Run in terminal
pip install django-rest-framework
-
Add
'rest_framework'
to INSTALLED APPS insettings.py
-
If it does not work, restart the editor(vscode or something)
Add your python file like C:UsershendrialqoryAppDataLocalProgramsPythonPython39-32Libsite-packages, Go to Setting Environment Variables and input your file python.
- Control+ shift + p.
- type ‘Python: Select Interpreter‘ and select the same.
- choose your virtual env from the list
if it is not listed please choose
Enter Interpreter path’ - Give pathe like this ‘c:usersuserdjangomyvenvscriptspython.ex’
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:
- delete the virtual environment folder and create it again.
which pip
must show the right path now in the new virtual env terminal.- reinstall the packages.
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.
Answers of @nayburz and @Faseela worked for me.
- Control+ shift + p.
- type ‘Python: Select Interpreter’ and select the same.
- choose your virtual env from the list if it is not listed please choose Enter Interpreter path’
- path of your virtual env python.exe file.
mine was : D:PythonPython_DjangotrydjangoenvScriptspython.exe
You can find similar path according to your project
for others using venv and if doing ‘Python:Selecting Interpreter’ and selecting your venv doesn’t work then try these steps. These are for Mac.
- open Terminal
- navigate to your project folder.
- activate virtual environment ( > source {path_to_venv_home}binactivate)
- now open vscode with ‘code .’
In case ‘code .’ doesn’t work then should add to your path.
Try ‘Launching from the command Line’ section from https://code.visualstudio.com/docs/setup/mac
If you have set all as mentioned here, and you still have that error. makesure you check in your code editor if you are in the directory where your virtual environement is found.
It is good to mention that, when you open your code editor (VS code for example) makesure your open at the directory where your Virtual environment (venv) is found. this will certainly be the case.