Subchapter 50.24
references/sdk/snippets/python-server-sdk.mdMarkdown3 KBView on GitHub
Use a current launchdarkly-server-sdk release; see the SDK releases page (opens in a new tab) and version compatibility (opens in a new tab) (Python 3.9+ from SDK 9.12+).
Install (Install the SDK (opens in a new tab)):
pip install launchdarkly-server-sdkOptional observability (Python observability (opens in a new tab)) — requires Python SDK 9.12+:
pip install launchdarkly-observabilityImport (same topic):
import ldclient
from ldclient.config import Config
# Optional — launchdarkly-observability package; requires Python SDK v9.12+
# from ldobserve import ObservabilityPluginInitialize: Call ldclient.set_config(Config(...)) once, then ldclient.get() for the singleton client (Initialize the client (opens in a new tab)). With observability: Config(sdk_key, plugins=[ObservabilityPlugin()]). Worker processes that fork may need postfork() (Considerations with worker-based servers (opens in a new tab)).
Includes: Minimal onboarding script. SDK key: LAUNCHDARKLY_SDK_KEY (Apply: environment configuration).
import os
import sys
import ldclient
from ldclient.config import Config
if __name__ == "__main__":
sdk_key = os.environ.get("LAUNCHDARKLY_SDK_KEY")
if not sdk_key or not sdk_key.strip():
print(
"LAUNCHDARKLY_SDK_KEY is not set. Use Project settings > Environments > SDK key.",
file=sys.stderr,
)
raise SystemExit(1)
ldclient.set_config(Config(sdk_key))
client = ldclient.get()
if not client.is_initialized():
print("LaunchDarkly client failed to initialize", file=sys.stderr)
raise SystemExit(1)
# For onboarding only — events are normally flushed in the background.
client.flush()
print("LaunchDarkly client ready.")