'Can't import rest_framework in Django

I'm trying to stand up a Django site and I am attempting to setup Report_builder with that. I just got the front end of report builder to work but it is not returning any data. I suspect the problem is with my rest framework. The settings did not auto create and now when I try to import it to create a serializer restframework is not recognized at all.

Here is my settings page:

  REST_FRAMEWORK = {
     'DEFAULT_AUTHENTICATION_CLASSES': (
         'rest_framework.authentication.SessionAuthentication',
     ),


 }

Did I possibly install rest framework in the wrong place or??
I have the latest version.



Solution 1:[1]

enter image description here

Try changing appropriate python environment (Virtual Env in your project folder) if even after

INSTALLED_APPS = [ ... 'rest_framework', .... ]

you are unable to import rest_framework.

Solution 2:[2]

In your setting.py you must have :

INSTALLED_APPS = [
   ...
   'rest_framework',
   ....
]

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
    'rest_framework.authentication.SessionAuthentication',
),
}

As describe at Tutorial of Django Rest Framework

Solution 3:[3]

Recently I faced the same problem and that is not only for the rest_framework. The answer for me is answered above but not fully how to check for beginners.

  • Make sure that you have initialized the framework to the settings.py You should have it under INSTALLED_APPS and REST_FRAMEWORK

The main problem you might face is the environments of python you have. For checking if you have it already installed run: python and import rest_framework That most likely will return you an error of ImportError: No module named rest_framework. So the fix for that is running it with python3.

In case you do not have it run pip install djangorestframework

Side note to that: check which version of djangorestframework you have as depending on the python you are running, as versions of rest_framework 1.9 onwards wont work with Python3

Solution 4:[4]

If you have followed the installation guide https://www.django-rest-framework.org/#installation from the documentation and it still not resolving the dependencies, try to restart your IDE. For me, Visual Studio Code did not recognize the new dependencies otherwise. After restart, the import works as expected.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Matheus Veleci
Solution 3 Daniel Simeonov
Solution 4 Pfinnn