Split.io Python Client

10.6.0 · active · verified Thu Apr 16

Split.io Python Client (splitio-client) is the official Python SDK for the Split Feature Delivery Platform. It enables developers to implement feature flags, conduct controlled rollouts, and perform data-driven experiments to manage the customer experience. The library is actively maintained by Split (now part of Harness) with frequent updates that include bug fixes and new feature support, such as rule-based segments and feature flag prerequisites. It is compatible with Python 3.7 and higher.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Split.io Python SDK, wait for it to be ready, and retrieve a treatment for a specific feature flag and user key. It also includes basic error handling for timeouts and ensures proper shutdown of the SDK factory.

import os
from splitio import get_factory
from splitio.exceptions import TimeoutException

# Get your SDK API key from environment variables for security
SPLIT_API_KEY = os.environ.get('SPLIT_SDK_API_KEY', 'YOUR_SDK_TYPE_API_KEY')
CUSTOMER_ID = 'test_user_123'
FEATURE_FLAG_NAME = 'my_new_feature'

# Initialize the Split factory
# For production, ensure config is properly set up, e.g., for logging, storage, etc.
factory = get_factory(SPLIT_API_KEY)

try:
    # Wait up to 5 seconds for the SDK to be ready and download definitions
    factory.block_until_ready(5)
    split_client = factory.client()

    # Get treatment for the customer
    treatment = split_client.get_treatment(CUSTOMER_ID, FEATURE_FLAG_NAME)

    if treatment == 'on':
        print(f"Feature '{FEATURE_FLAG_NAME}' is ON for user '{CUSTOMER_ID}'.")
        # Code to show 'on' treatment
    elif treatment == 'off':
        print(f"Feature '{FEATURE_FLAG_NAME}' is OFF for user '{CUSTOMER_ID}'.")
        # Code to show 'off' treatment
    else:
        # 'control' or any other undefined treatment
        print(f"Feature '{FEATURE_FLAG_NAME}' is in CONTROL for user '{CUSTOMER_ID}'.")
        # Code for default/control treatment

except TimeoutException:
    print("Split SDK timed out while initializing. Falling back to control.")
    # Handle timeout gracefully, e.g., log and proceed with default experience
except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # It's important to destroy the factory when the application shuts down
    # to ensure all queued impressions/events are sent and resources are released.
    factory.destroy()

view raw JSON →