Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Django REST Framework cannot import rest_framework_jwt for DEFAULT_AUTHENTICATION_CLASSES

ErrorImportError: Could not import 'rest_framework_jwt.authentication.JSONWebTokenAuthentication' for API setting 'DEFAULT_AUTHENTICATION_CLASSES'.

Short answer

DRF imports every class path in DEFAULT_AUTHENTICATION_CLASSES at startup, and the error hides the real ImportError. Import the module by hand to see it. The package django-rest-framework-jwt is archived and lists support only up to Django 1.11, so replace it with djangorestframework-simplejwt.

  • Applies to Django 5.1, Django REST Framework 3.15, djangorestframework-simplejwt, Python 3.12
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    DRF wraps the real failure in a generic message. Import the same module yourself, in the same Python environment that runs Django, and read the last line of the traceback.

    python -c "import rest_framework_jwt.authentication"
  2. 2Match what you see, then jump to its fix

    1. 1ModuleNotFoundError: No module named 'rest_framework_jwt'The package is not installed in the environment that runs DjangoFix
    2. 2The module is installed, but the import still fails with a different ImportError on a Django or DRF nameThe old JWT package is archived and no longer matches current DjangoFix
    3. 3The new package is installed, but REST_FRAMEWORK still lists rest_framework_jwtThe settings and URLs still point at the old packageFix

What does the error look like?

  • ImportError: Could not import 'rest_framework_jwt.authentication.JSONWebTokenAuthentication' for API setting 'DEFAULT_AUTHENTICATION_CLASSES'.
  • ModuleNotFoundError: No module named 'rest_framework_jwt'

Cause 1 of 3

The package is not installed in the environment that runs Django

1Check

Server, IDE and shell often use different Python environments. Check that pip and python belong to the same virtual environment, and list what it has.

python -m pip --version
python -m pip list | grep -i -E "jwt|rest"

2Fix

Install into the active virtual environment with python -m pip, never with a bare pip that may belong to another Python. For a new project, skip the old package and install Simple JWT instead (see the next cause).

python -m pip install djangorestframework-simplejwt

3Verify

The import succeeds with no output.

python -c "import rest_framework_simplejwt.authentication"

Confirmed by docs.djangoproject.com

Did this fix it?

Cause 2 of 3

The old JWT package is archived and no longer matches current Django

1Check

The repository of django-rest-framework-jwt says the project is unmaintained and was archived in 2020. Its supported list ends at Python 3.6, Django 1.11 and DRF 3.6. Django 4.0 removed APIs such as django.utils.translation.ugettext, which code written for old Django may still import.

python -m pip show djangorestframework-jwt
python -m django --version

2Fix

Remove the old package and move to djangorestframework-simplejwt, which lists support for current Django and DRF. Change the login route and the client, because the token endpoint and response differ.

python -m pip uninstall djangorestframework-jwt
python -m pip install djangorestframework-simplejwt

3Verify

Start the server; the ImportError no longer appears.

python manage.py check

Confirmed by github.com

Did this fix it?

Cause 3 of 3

The settings and URLs still point at the old package

1Check

Search the project for the old module name. DRF imports each string in DEFAULT_AUTHENTICATION_CLASSES at startup, so one stale path stops every request.

grep -rn "rest_framework_jwt" --include=*.py .

2Fix

Point the setting at Simple JWT's class and add its token views to the URL config.

# settings.py
REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES": (
        "rest_framework_simplejwt.authentication.JWTAuthentication",
    ),
}

# urls.py
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView

urlpatterns = [
    path("api/token/", TokenObtainPairView.as_view(), name="token_obtain_pair"),
    path("api/token/refresh/", TokenRefreshView.as_view(), name="token_refresh"),
]

3Verify

Ask for a token, then call a protected route with it in the Authorization header.

curl -X POST http://127.0.0.1:8000/api/token/ -d "username=demo" -d "password=demo-pass"

Confirmed by django-rest-framework-simplejwt.readthedocs.io

Did this fix it?

How do I stop it coming back?

  • Pin dependencies in requirements.txt and check a package's repository for an archived notice before adding it.
  • Install and run everything through python -m pip and python -m in one virtual environment.
  • Run python manage.py check in CI so a bad authentication class fails the build, not the first request.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.