88👍
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.
8👍
- 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:\users\user\django\myvenv\scripts\python.ex’
- [Django]-Running django tests with sqlite
- [Django]-How to decode url to path in python, django
- [Django]-Django form multiple choice
6👍
-
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)
- [Django]-How can I register a single view (not a viewset) on my router?
- [Django]-Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding, when trying to start uwsgi
- [Django]-How do I iterate over the options of a SelectField in a template?
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:
- 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.
- [Django]-Using only the DB part of Django
- [Django]-Django form multiple choice
- [Django]-Difference between APIView class and viewsets class?
2👍
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:\Python\Python_Django\trydjango\env\Scripts\python.exe
You can find similar path according to your project
- [Django]-Filter Django database for field containing any value in an array
- [Django]-How to start doing TDD in a django project?
- [Django]-Django.db.utils.OperationalError Could not connect to server
1👍
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}\bin\activate)
- 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
- [Django]-Developing with Django+Celery without running `celeryd`?
- [Django]-Can django's auth_user.username be varchar(75)? How could that be done?
- [Django]-Django no such table:
1👍
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.
- [Django]-How do I get user IP address in Django?
- [Django]-Django unit tests without a db
- [Django]-Django Rest Framework How to update SerializerMethodField
0👍
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.
- [Django]-Django admin interface: using horizontal_filter with inline ManyToMany field
- [Django]-Search by foreign key id in admin
- [Django]-Django update table using data from another table
0👍
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.
- [Django]-Issue with ManyToMany Relationships not updating immediately after save
- [Django]-Django model blank=False does not work?
- [Django]-How does manage.py work?
0👍
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:
- Delete the current virtual environment
- Navigate to the directory where your virtual environment should located.
- Create a new one by running the command:
python3 -m venv myvenv
- Activate the virtual environment by running the command:
source venv/bin/activate
- If all goes well, then you should be able to see the name of your virtual environment appear in the terminal prompt. For example,
(myvenv) user@computer:~$
- Now, running
which pip
should output something like/path/to/myvenv/bin/pip
as confirmation that your virtual environment is set and activated. - You can now reinstall your
rest_framework
package aspip install djangorestframework
, and the pylance should be able to recogniserest_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.
- [Django]-How do I use django rest framework to send a file in response?
- [Django]-Put a <a> hyperlink in a django message
- [Django]-When to use Django get_absolute_url() method?
0👍
Here’s what I solved my problem:
- Activate django virtual environment by "pipenv shell"
- Make sure I installed restframework library, by "pip install djangorestframework"
- Ctrl/Command + Shift + P to call interpreter and select the one with current projects
- [Django]-Get router url name when testing in Django Rest Framework
- [Django]-Django Rest Framework custom response message
- [Django]-Django admin and showing thumbnail images