Flagsmith Python SDK
The Flagsmith Python SDK enables you to integrate feature flags and remote configuration into your Python applications. It allows for managing features across multiple projects, environments, and organizations, facilitating controlled rollouts and A/B testing. The current version is 5.1.1, and it typically sees regular releases with bug fixes, features, and dependency updates.
Warnings
- breaking Flagsmith Python SDK version 3.5.0 introduced a dependency on Pydantic V2. Projects still using Pydantic V1 will encounter compatibility issues.
- breaking Major versions 4.0.0 and 5.0.0 included breaking changes. The changelog on GitHub should be consulted for specific migration steps. These often involve updates to the SDK's internal engine or API interactions.
- gotcha Server-side SDKs require a Server-side Environment Key, which starts with `ser.`. Using a Client-side key will lead to authentication failures or incorrect behavior. These keys should be treated as secret.
- gotcha When using `enable_local_evaluation=True`, the SDK fetches the environment document in a separate thread. Ensure proper initialization and shutdown procedures to avoid resource leaks or outdated flag evaluations. The `close()` method should be called when the application exits.
- gotcha Remote Evaluation mode makes an API call for every `get_environment_flags()` or `get_identity_flags()` request, which can introduce latency. Local Evaluation mode can improve performance but evaluates flags based on a locally cached environment document.
Install
-
pip install flagsmith
Imports
- Flagsmith
from flagsmith import Flagsmith
Quickstart
import os
from flagsmith import Flagsmith
# Replace 'YOUR_ENVIRONMENT_KEY' with your actual Flagsmith Environment Key
# It's recommended to use environment variables for keys in production.
# Server-side Environment Keys begin with 'ser.'
environment_key = os.environ.get('FLAGSMITH_ENVIRONMENT_KEY', 'YOUR_ENVIRONMENT_KEY')
# Initialize Flagsmith client
# enable_local_evaluation=True is recommended for production for performance
# and resilience, as it polls the API in a separate thread.
flagsmith = Flagsmith(
environment_key=environment_key,
enable_local_evaluation=True
)
# Wait for initial flags to be loaded if using local evaluation
# In a real application, you might want to handle this asynchronously
# or with a proper startup sequence.
flagsmith.update_environment()
# Get all flags for the environment
environment_flags = flagsmith.get_environment_flags()
# Check if a feature is enabled
if environment_flags.is_feature_enabled('my_feature'):
print("My feature is enabled!")
# Get a feature value
feature_value = environment_flags.get_feature_value('another_feature')
print(f"Value of 'another_feature': {feature_value}")
# Get flags for an identity
# Traits will automatically be persisted against the identity in Remote Evaluation mode.
# In Local Evaluation mode, only traits provided at runtime are used.
identity_flags = flagsmith.get_identity_flags(identifier='test_user_123', traits={'plan': 'premium'})
if identity_flags.is_feature_enabled('premium_feature'):
print("Premium feature is enabled for test_user_123!")
# Clean up / close the Flagsmith client (important for local evaluation threads)
flagsmith.close()