4

I'm getting this error in VS Code:

error: "Unable to import 'rest_framework'pylint(import-error)"

I've installed djangorestframework with pip in my virtual environment. I activated the environment before installing it but when I try to import django_rest_framework in my app "book", it says that can't import it. I've added the rest framework in my installed apps but I don't know which is the problem.

I'm working with django 2.1.5 and djangorestframework 3.9.3 and python 3.6.7 and pip3 19.1.1 I've checked with pip freeze and it's installed. The virtual environment is activated.

What could be the problem? I opened in PyCharm but i got the same error

Code from I'm getting the importation error in my serializer.py file that is in my app called "book"

from rest_framework import viewsets

from .models import Book
from .serializer import BookSerializer

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'book',

]
Manu mathew
  • 859
  • 8
  • 25
Eduardo Reyes
  • 103
  • 1
  • 2
  • 7

5 Answers5

10

I think you are not using your virtual environment in VSCode. You can update your workspace settings like this:

{
    "python.defaultInterpreterPath": "/path/to/your/venv/bin/python",
}

Or you can choose an environment from vscode.

ruddra
  • 50,746
  • 7
  • 78
  • 101
  • In short, 1. Open VSCode 2. Press Control+Shift+P 3. Type Language and Open Configure Language Specific Settings 4, Add the above mentioned python path configuration – Anand Sonawane Apr 08 '21 at 15:47
  • 1
    Just wanted to add, also change: "python.defaultInterpreterPath" : "/path/to/your/venv/bin/python" –  Mar 29 '22 at 09:00
1

Did you install proper package ? The syntax for installing Django Rest Framework is:

pip install djangorestframework

which is confusing sometimes becouse in INSTALLED_APPS you type 'rest_framewor'. Check if you installed djangorestframework not for example: django-rest-framework.

Mikey
  • 400
  • 4
  • 11
1

I have got almost the same probleme: djangorestframework installed and checked on freeze, rest_framework on settings.py and I got the same issue "reportmissingimports".

The solution is to chose the python interpreter :

  • on VSCode: CTR + SHIFT + P
  • then click on Python: Select interpreter
  • then you will got several Python path (ex: Python ('env':venv),Python 3.8 ..... Global, Python 3.8.....)
Jynadal
  • 111
  • 1
  • 5
0

From a similar experience with pylint, try re-creating the virtual environment. Simply delete the virtual environment directory and re-create it

MarvinKweyu
  • 180
  • 4
  • 12
0

The solution is to chose the python interpreter :

on VSCode: CTR + SHIFT + P then click on Python: Select interpreter then you will got several Python path (ex: Python ('env':venv),Python 3.8 ..... Global, Python 3.8.....) I choose conda one of them and it started working now.