Unleash Python Client SDK
The Unleash Python Client SDK is a server-side client for the Unleash feature management platform, enabling developers to integrate and evaluate feature flags within their Python applications. It connects to an Unleash server (Enterprise or Open Source) to fetch flag configurations and evaluate them locally against an Unleash context. Currently at version 6.7.0, the library maintains an active release cadence with frequent updates and bug fixes.
Warnings
- breaking Direct access to `UnleashClient.features` was removed in v6.0.0. The internal representation of feature flags is no longer publicly accessible.
- breaking Custom activation strategies require an updated interface in v6.0.0. They no longer inherit from a base class, and the `apply` method's signature changed to `apply(self, parameters: dict, context: dict = None) -> bool`.
- deprecated The `default_value` argument in the `is_enabled()` method was deprecated in v4.0.0.
- gotcha Upon initialization, feature flags will evaluate to `False` until the client has successfully synchronized with the Unleash API. This can lead to unexpected behavior during application startup.
- gotcha The SDK runs a background thread for fetching feature flags and sending metrics. This requires special consideration in multi-process environments like WSGI servers (e.g., Gunicorn, uWSGI) or Celery workers.
Install
-
pip install UnleashClient
Imports
- UnleashClient
from UnleashClient import UnleashClient
Quickstart
import os
from UnleashClient import UnleashClient
UNLEASH_URL = os.environ.get('UNLEASH_API_URL', 'http://localhost:4242/api')
UNLEASH_APP_NAME = os.environ.get('UNLEASH_APP_NAME', 'my-python-app')
UNLEASH_INSTANCE_ID = os.environ.get('UNLEASH_INSTANCE_ID', 'instance-1')
UNLEASH_API_TOKEN = os.environ.get('UNLEASH_API_TOKEN', 'YOUR_API_TOKEN_HERE') # Typically a server-side token
# Initialize the client
client = UnleashClient(
url=UNLEASH_URL,
app_name=UNLEASH_APP_NAME,
instance_id=UNLEASH_INSTANCE_ID,
custom_headers={'Authorization': UNLEASH_API_TOKEN}
)
# Start the client (connects to Unleash server, fetches flags, etc.)
client.initialize_client()
# Check if a feature is enabled
enabled = client.is_enabled("my_feature_flag")
print(f"Feature 'my_feature_flag' is enabled: {enabled}")
# Check with context
user_context = {
"userId": "123",
"sessionId": "abc",
"properties": {"region": "us-east"}
}
enabled_for_user = client.is_enabled("user_specific_feature", user_context)
print(f"Feature 'user_specific_feature' is enabled for user 123: {enabled_for_user}")
# Get a variant
variant = client.get_variant("variant_toggle", user_context)
print(f"Variant for 'variant_toggle': {variant}")
# Gracefully shut down the client when no longer needed
client.destroy()