Harshil Gambhir
Harshil Gambhir

Reputation: 397

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'
]

Upvotes: 29

Views: 39504

Answers (15)

Vinay Sharma
Vinay Sharma

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:

  1. Open Command Palette:
    • Press Ctrl + Shift + P (or Cmd + Shift + P on macOS).
  2. Rebuild Index:
    • Type and select Python: Restart Language Server. This will restart the Pylance language server and rebuild the index.

Upvotes: 0

Aliaksandr
Aliaksandr

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

rohit
rohit

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

AbirZayn
AbirZayn

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

  1. ctrl + shift + p
  2. Click on Find
  3. Check on the project/venv(virtual env)/Scripts/python & Select it as the Interpreter.

Upvotes: 0

Rayzz
Rayzz

Reputation: 11

Here's what I solved my problem:

  1. Activate django virtual environment by "pipenv shell"
  2. Make sure I installed restframework library, by "pip install djangorestframework"
  3. Ctrl/Command + Shift + P to call interpreter and select the one with current projects

Upvotes: 1

Domo Dan
Domo Dan

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:

  1. Delete the current virtual environment
  2. Navigate to the directory where your virtual environment should located.
  3. Create a new one by running the command: python3 -m venv myvenv
  4. Activate the virtual environment by running the command: source venv/bin/activate
  5. 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:~$
  6. Now, running which pip should output something like /path/to/myvenv/bin/pip as confirmation that your virtual environment is set and activated.
  7. You can now reinstall your 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

user20370068
user20370068

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

Srinivasa Poduri
Srinivasa Poduri

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.

  1. open Terminal
  2. navigate to your project folder.
  3. activate virtual environment ( > source {path_to_venv_home}\bin\activate)
  4. 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

Upvotes: 2

Sahil Garg
Sahil Garg

Reputation: 43

Answers of @nayburz and @Faseela worked for me.

  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. 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

Upvotes: 2

TWIGA
TWIGA

Reputation: 11

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.

Upvotes: 0

Matin Sasan
Matin Sasan

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:

  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.

Upvotes: 5

Faseela Thayattuchira
Faseela Thayattuchira

Reputation: 527

  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'

Upvotes: 11

user16792619
user16792619

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

Rahal Athukorala
Rahal Athukorala

Reputation: 99

  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)

Upvotes: 7

nayburz
nayburz

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

Related Questions