Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: Could not import 'rest_framework_simplejwt.authentication.JWTAuthentication'

I am trying to deploy a django app on GCP but when i try to make migrations it gives me this error:

ImportError: Could not import 'rest_framework_simplejwt.authentication.JWTAuthentication' for API setting 'DEFAULT_AUTHENTICATION_CLASSES'. ModuleNotFoundError: No module named 'rest_framework_simplejwt'.

Settings.py

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    ],
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated'
    ],
'DEFAULT_RENDERER_CLASSES': [
        'rest_framework.renderers.JSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
    ]
}

SIMPLE_JWT = {
    'ALGORITHM': 'HS256',
    'SIGNING_KEY': SECRET_KEY,
    'VERIFYING_KEY': None,
'ACCESS_TOKEN_LIFETIME': timedelta(minutes=800),
    'REFRESH_TOKEN_LIFETIME': timedelta(days=2),
}
OAUTH2_PROVIDER = {
        'ACCESS_TOKEN_EXPIRE_SECONDS': 60 * 15,
        'OAUTH_SINGLE_ACCESS_TOKEN': True,
        'OAUTH_DELETE_EXPIRED': True
 }

requirements.txt

django-cors-headers
pyjwt
djangorestframework
djangorestframework-jwt==1.11.0

What is it that I am missing ?

UPDATE I installed rest_framework_simplejwt and now the error shifted to :

No module named 'rest_framework_simplejwt.tokens'

like image 430
Rahul Sharma Avatar asked Sep 10 '19 05:09

Rahul Sharma


2 Answers

It seems you are confusing two packages. djangorestframework-jwt which you have in your requirements.txt is no longer maintained. It provides the rest_framework_jwt.authentication.JSONWebTokenAuthentication authentication class.

However, the one you are actually using, rest_framework_simplejwt.authentication.JWTAuthentication, comes from the pip package djangorestframework-simplejwt

So you need to update your requirements.txt. Remove djangorestframework-jwt and add djangorestframework-simplejwt

like image 183
Bary12 Avatar answered Oct 06 '22 00:10

Bary12


for me the djangorestframework-simplejwt upgrade from 4.4.0 to current latest version 4.6.0 fixed the problem.

pip3 install --upgrade djangorestframework-simplejwt
like image 43
Ali Aref Avatar answered Oct 06 '22 00:10

Ali Aref