{"id":1303,"library":"launchdarkly-server-sdk","title":"LaunchDarkly Python Server SDK","description":"The LaunchDarkly SDK for Python provides functionality to integrate feature flags into your Python applications. It enables developers to control feature rollouts, perform A/B testing, and manage configurations without redeploying code. The library is actively maintained with frequent minor and patch releases, typically every 1-2 months, to introduce new features, improvements, and bug fixes.","status":"active","version":"9.15.0","language":"en","source_language":"en","source_url":"https://github.com/launchdarkly/python-server-sdk","tags":["feature flags","a/b testing","sdk","feature management","configuration"],"install":[{"cmd":"pip install launchdarkly-server-sdk","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Used for streaming updates from the LaunchDarkly service.","package":"eventsource","optional":false}],"imports":[{"note":"The `ldclient` module directly (without `_server_sdk`) is an older pattern. It's best to import `LdClient` and `Config` explicitly from `launchdarkly_server_sdk`.","wrong":"import ldclient; client = ldclient.get()","symbol":"LdClient","correct":"from launchdarkly_server_sdk import LdClient, Config"}],"quickstart":{"code":"import os\nfrom launchdarkly_server_sdk import LdClient, Config\n\n# Replace with your actual SDK key from LaunchDarkly\nSDK_KEY = os.environ.get('LAUNCHDARKLY_SDK_KEY', 'YOUR_SDK_KEY_HERE')\n\nif not SDK_KEY or SDK_KEY == 'YOUR_SDK_KEY_HERE':\n    print(\"Warning: LAUNCHDARKLY_SDK_KEY environment variable not set or default used. Client will be in offline mode.\")\n    # In a real application, you might raise an error or handle this differently.\n    config = Config(offline=True)\nelse:\n    config = Config()\n\n# Initialize the LaunchDarkly client\nld_client = LdClient(SDK_KEY, config)\n\n# Wait for the client to be initialized (recommended for server-side apps)\nif ld_client.is_initialized():\n    print(\"LaunchDarkly client initialized!\")\n\n    # Define a user for flag evaluation\n    user = {\n        \"key\": \"example-user-key\",\n        \"name\": \"Example User\",\n        \"custom\": {\n            \"group\": \"beta_testers\"\n        }\n    }\n\n    # Evaluate a feature flag\n    flag_value = ld_client.variation(\"my-feature-flag\", user, False)\n    print(f\"Feature flag 'my-feature-flag' is: {flag_value} for user {user['key']}\")\n\n    # Don't forget to shut down the client when your application exits\n    ld_client.close()\n    print(\"LaunchDarkly client closed.\")\nelse:\n    print(\"LaunchDarkly client failed to initialize.\")\n    # Ensure the client is closed even if initialization fails\n    ld_client.close()\n","lang":"python","description":"This quickstart demonstrates how to initialize the LaunchDarkly client, define a user, evaluate a feature flag, and properly shut down the client. It uses an environment variable for the SDK key for security. In production, ensure the client is initialized and shut down gracefully with your application's lifecycle."},"warnings":[{"fix":"Upgrade your Python environment to 3.10 or newer. If you must use Python 3.9, pin the SDK version to `<9.15.0` (e.g., `launchdarkly-server-sdk<9.15.0`).","message":"Python 3.9 is no longer supported starting with version 9.15.0.","severity":"breaking","affected_versions":">=9.15.0"},{"fix":"Upgrade your Python environment to 3.9 or newer. If you must use Python 3.8, pin the SDK version to `<9.12.0` (e.g., `launchdarkly-server-sdk<9.12.0`).","message":"Python 3.8 is no longer supported starting with version 9.12.0.","severity":"breaking","affected_versions":">=9.12.0"},{"fix":"If you are using the file data source and directly manipulating `ChangeSet` objects, ensure that every change operation includes a valid `Selector` as specified in the updated API documentation.","message":"The `ChangeSet` object in the file data source now strictly requires a `Selector` for updates.","severity":"gotcha","affected_versions":">=9.15.0"},{"fix":"Always use a valid LaunchDarkly SDK key. Validate your SDK key format if you encounter unexpected client initialization issues on versions 9.12.1 or newer. An SDK key usually starts with 'sdk-'","message":"SDK key validation was added, which performs a basic format check on initialization. Providing an invalid SDK key format may lead to immediate client initialization failure.","severity":"gotcha","affected_versions":">=9.12.1"},{"fix":"Integrate `ld_client.close()` into your application's shutdown process, for example, using a `finally` block or a signal handler in web frameworks.","message":"It is crucial to call `ld_client.close()` when your application is shutting down to ensure all pending events are flushed and resources are released. Failing to do so can lead to lost analytics events or resource leaks.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-08T00:00:00.000Z","next_check":"2026-07-07T00:00:00.000Z"}