unable to import rest_framework_simplejwt

Viewed 3084

I have installed djangorestframework-simplejwt package and trying to import that module in urls.py and views.py but still its not working. Please guide me to solve this issue.

pip list

Package                       Version
----------------------------- -------
asgiref                       3.3.1
Django                        3.1.4
djangorestframework           3.12.2
djangorestframework-simplejwt 4.6.0
pip                           20.2.3
PyJWT                         1.7.1
pytz                          2020.4
setuptools                    49.2.1
sqlparse                      0.4.1

Settings.py

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'Django_MedicalApp',
'rest_framework',
'rest_framework_simplejwt',]


REST_FRAMEWORK = {'DEFAULT_AUTHENTICATION_CLASSES':
                  ['rest_framework_simplejwt.authentication.JWTAuthentication',],
              'DEFAULT_PERMISSION_CLASSES': 
('rest_framework.permissions.AllowAny','rest_framework.permissions.IsAuthenticatedOrReadOnly',)}

urls.py

from rest_framework_simplejwt.views import TokenObtainPairView
router = routers.DefaultRouter()
router.register('Company',views.CompanyViewset,basename='Company')


urlpatterns = [
path('admin/', admin.site.urls),
path('api/',include(router.urls),
path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'))]

views.py

from rest_framework_simplejwt.authentication import JWTAuthentication

class CompanyViewset(viewsets.ViewSet):
    authentication_classes = [JWTAuthentication]

Please help me to resolve this import error.

2 Answers

change your interpreter to something else and then choose your virtual env back again. it will work 100%. After installing the simplejwt the interpreter needs to be reset which is achieved by changing the env to something else and back to the virtual env you use normally

select interpreter

This stems from your Python interpreter. If you are using a virtual environment, please try to select a different venv. In my case

1- I'm using my own venv and import is not successful:

enter image description here

2- However, when I'm using the global python interpreter, import is okay:

enter image description here

The reason that it works like that probably we both have something wrong in our venvs.

Related