Django REST Framework cannot import rest_framework_jwt for DEFAULT_AUTHENTICATION_CLASSES
ImportError: 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.
Fix it fast
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"2Match what you see, then jump to its fix
- 1
ModuleNotFoundError: No module named 'rest_framework_jwt'The package is not installed in the environment that runs DjangoFix - 2
The 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
The new package is installed, but REST_FRAMEWORK still lists rest_framework_jwtThe settings and URLs still point at the old packageFix
- 1
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-simplejwt3Verify
The import succeeds with no output.
python -c "import rest_framework_simplejwt.authentication"Confirmed by docs.djangoproject.com
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 --version2Fix
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-simplejwt3Verify
Start the server; the ImportError no longer appears.
python manage.py checkConfirmed by github.com
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
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:
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- MenuloA restaurant discovery platform in Germany, kept current by a Python ingestion pipeline.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Back end
Airflow SSHOperator stops a long script with SSH command timed out
airflow.exceptions.AirflowException: SSH command timed out
SSHOperator waits only 10 seconds for a command by default.
3 causes, each with a fixBack end
Python asyncio: Task was destroyed but it is pending
RuntimeWarning: coroutine '...' was never awaited
A task was still unfinished when Python threw it away.
4 causes, each with a fixBack end
Celery in a Django project does not connect to Redis or ignores the broker URL
kombu.exceptions.InconsistencyError: ... _kombu.binding.celery
Celery talks to Redis through a client library and reads its broker URL from your Django settings.
4 causes, each with a fixBack end
Django form POST returns 403 Forbidden: CSRF verification failed
CSRF verification failed. Request aborted.
Django rejects an unsafe request when the CSRF cookie or the form token is missing or wrong, or when the Origin or Referer check fails.
5 causes, each with a fixBack end
Django cannot import corsheaders (django-cors-headers)
ImportError: No module named corsheaders
The pip package is django-cors-headers, but the module Django imports is corsheaders.
3 causes, each with a fixBack end
Django raises NoReverseMatch when building a URL
NoReverseMatch
NoReverseMatch means Django looked for a named URL pattern that fits the name and arguments you gave and found none.
4 causes, each with a fix