optional-django

raw JSON →
0.3.0 verified Fri May 01 auth: no python

Utilities for providing optional support for Django in Python packages, allowing conditional feature loading depending on whether Django is installed. Version 0.3.0 appears to be the latest; release cadence is low (last release years ago).

pip install optional-django
error ImportError: No module named optional_django
cause Library not installed.
fix
Run 'pip install optional-django'.
error AttributeError: module 'optional_django' has no attribute 'is_available'
cause Using an older version of the library.
fix
Upgrade to version 0.3.0: 'pip install --upgrade optional-django'.
error django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured.
cause Trying to use Django features without setting up Django settings.
fix
Call 'django.setup()' or set the DJANGO_SETTINGS_MODULE environment variable before using Django.
gotcha The library only checks whether Django is installed, not a specific version. If your package requires a minimum Django version, you must check separately.
fix Use pkg_resources or importlib.metadata to check Django version independently.
deprecated The library has not been updated in years and may not work with modern Django versions (e.g., 4.x, 5.x). Verify compatibility in your environment.
fix Consider using try/except ImportError blocks instead of this library for simplicity.
gotcha Using this library does not prevent Django from being a top-level dependency. You still need to package Django in extras_require if you want optional support.
fix Use extras_require in setup.py/setup.cfg to keep Django optional.

Basic usage: check availability and conditionally import Django components.

import optional_django

# Check if Django is available
print(optional_django.is_available())  # True or False

# Conditionally import Django models
if optional_django.is_available():
    from django.db import models
    print('Django models available')