Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why im getting an error at importing django rest framework?

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

]
like image 918
Eduardo Reyes Avatar asked Oct 18 '25 15:10

Eduardo Reyes


2 Answers

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.

like image 171
ruddra Avatar answered Oct 21 '25 05:10

ruddra


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.

like image 32
Mikey Avatar answered Oct 21 '25 04:10

Mikey