简体   繁体   中英

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

]

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

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

Or you can choose an environment from vscode .

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

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 .

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

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.

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

]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM