Reputation: 397
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'
]
Upvotes: 29
Views: 39504
Reputation: 91
I checked, and I had created a virtual environment and connected successfully, but I was still unable to resolve the error but this given below solution workout for me.
Rebuild Pylance Index If the issue persists, you may try rebuilding the Pylance index:
Upvotes: 0
Reputation: 1
I solved this problem in PyCharm by installing this package from PyCharm directly:
Settings -> Project: <project_name> -> Python Interpreter -> "+" -> (In the search field) djangorestframework
When I previously installed this package with pip, I couldn't find it in the PyCharm interpreter installed packages. Now I see it.
Upvotes: 0
Reputation: 1
If you are using docker, then try deleting container and then corresponding image. Build container again using docker-compose up
Error might be possible due to requirements.txt present in container is not updated. (immutable)
Upvotes: 0
Reputation: 150
Posted on 10 January 2024
To Fix this , From the VS Code editor press ctrl + shift + p
following that Select the interpreter Global one.
If this not resolve the solution than check if the virtual environment is activate or not. If this is activated then press
ctrl + shift + p
Upvotes: 0
Reputation: 11
Here's what I solved my problem:
Upvotes: 1
Reputation: 1
For those using VSCode, if selecting the Python Interpreter still failed and also, when you run which pip
, the result is not pointing to your virtual environment, something path/to/myvenv/bin/pip
, then do the following:
python3 -m venv myvenv
source venv/bin/activate
(myvenv) user@computer:~$
which pip
should output something like /path/to/myvenv/bin/pip
as confirmation that your virtual environment is set and activated.rest_framework
package as pip install djangorestframework
, and the pylance should be able to recognise rest_framework
At least, that worked for me when I landed into that same linter issues. Also, note that you will need to activate the virtual environment every time you open a new terminal window before running any command, say pip, python
and the others.
Upvotes: 0
Reputation: 11
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.
Upvotes: 1
Reputation: 35
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.
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
Upvotes: 2
Reputation: 43
Answers of @nayburz and @Faseela worked for me.
mine was : D:\Python\Python_Django\trydjango\env\Scripts\python.exe
You can find similar path according to your project
Upvotes: 2
Reputation: 11
I was having similar problem.
Upvotes: 0
Reputation: 1885
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:
which pip
must show the right path now in the new virtual env terminal.Upvotes: 5
Reputation: 527
Upvotes: 11
Reputation: 1
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.
Upvotes: 0
Reputation: 99
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)
Upvotes: 7
Reputation: 1036
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.
Upvotes: 102