Skip to content

ModuleNotFoundError: No module named 'django.test' when running without test dependencies (e.g., Poetry --without test) #969

@mpyatishev

Description

@mpyatishev

In the latest version (5.5.1), rest_framework_simplejwt/settings.py unconditionally imports setting_changed from django.test.signals.
In many production environments or Docker builds using package managers like Poetry with the --without test flag, the django.test module is not installed. This causes the application to crash on startup during the Django setup phase.

File "/.../rest_framework_simplejwt/models.py", line 7, in <module>
    from .settings import api_settings
File "/.../rest_framework_simplejwt/settings.py", line 5, in <module>
    from django.test.signals import setting_changed
ModuleNotFoundError: No module named 'django.test'

Environment:
Python: 3.13 (or any version where django.test is excluded)
Django: 5.1+
djangorestframework-simplejwt: 5.5.1
Installation Method: poetry install --without test

Suggested Fix:
The import should be wrapped in a try...except block or moved to a location where it is only called during testing. Alternatively, it could be imported from django.core.signals if applicable, as setting_changed is also available there in modern Django versions.

try:
    from django.test.signals import setting_changed
except ImportError:
    # Django < 5.1 compatibility or environments without django.test
    from django.core.signals import setting_changed

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions