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

Question:

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’

Asked By: Rahul Sharma

||

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

Answered By: Bary12

If anyone still encountering this error, this is the requirements for djangorestframework-simplejwt:

Python (3.6, 3.7, 3.8)
Django (2.0, 2.1, 2.2, 3.0)
Django REST Framework (3.8, 3.9, 3.10)

I downgraded Django & DRF and the problem is solved for me.

pip uninstall django
pip uninstall djangorestframework
pip install --upgrade django==3.0
pip install --upgrade djangorestframework==3.10
pip install djangorestframework-simplejwt
Answered By: Jan Franco

I encountered same error when attempting to deploy on Heroku

Upon investigating I found 2 issues in my case:

1] requirements.txt –> This file was not updated and therefore ‘git add’ and ‘git commit’ did not pickup the new requirement for djangorestframework-simplejwt

So the solution here was to do a fresh pip freeze > requirements.txt, git add/commit and confirm that on Heroku

2] Secondly I found that Heroku was not finding the v4.6.0 which my python3.8 had installed locally. Instead, I had to edit the requirements.txt to v4.4.0 (current on the pypi.org), redo the git add/commit before this problem was resolved

Answered By: Sandy

For me issue solverd by restarting virtual environment

Answered By: Fuad Teymurov

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
Answered By: Ali Aref

If anyone is still running into this issue using VS Code and running their Django app inside a Docker container, I solved the issue by downgrading my Python version to 3.9, instead of 3.10, and then rebuilding my container.

In your Dockerfile make sure the FROM python:3.9 matches the current supported version of python on the docs page: https://django-rest-framework-simplejwt.readthedocs.io/en/latest/index.html , in this case, version 3.9. Mine was set to 3.10 before, which at the time of this writing, isn’t supported. Then re-build your docker image.

docker-compose down

docker-compose up -d --build

Also, if you are using pipenv to manage your packages, change your python version in your Pipfile, python_version = "3.9". Then run pipenv install to install the change.

Answered By: Evan Bloemer

My issue was also having installed both djangorestframework-jwt and djangorestframework_simplejwt. After unistalling the former my issue was resolved.

Answered By: Carla Aluvai

You need to restart your machine and install

  • pip install djangorestframework-jwt
  • pip install djangorestframework-simplejwt

please ensure those lib’s install or not

Answered By: shine

Depleting virtual environment and creating a new one, install the supported version of drf solved this for me

Answered By: Jibril14