This topic documents how to get started with the Python SDK, and links to reference information on all of the supported features.
LaunchDarkly’s SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and sample applications:
The LaunchDarkly Python SDK, version 9.0 through 9.11, is compatible with Python 3.8.0 and higher. Starting with version 9.12, the SDK requires Python 3.9.0 or higher.
After you complete the Getting Started process, follow these instructions to start using the LaunchDarkly SDK in your Python application.
First, install the LaunchDarkly SDK as a dependency in your application using your application’s dependency manager. If you want to depend on a specific version, refer to the SDK releases page to identify the latest version.
We recommend making the LaunchDarkly observability plugin available as well. This plugin collects and sends observability data to LaunchDarkly, including metrics autogenerated from OpenTelemetry data. This means you can review error monitoring, logs, and traces from within the LaunchDarkly UI. They require the Python SDK version 9.12 or later.
Here’s how:
Next, import the LaunchDarkly client in your application code:
The Python SDK uses an SDK key. Keys are specific to each project and environment. They are available on the SDK keys page under Settings. To learn more about key types, read Keys.
After you install and import the SDK, create a single, shared instance of ldclient. Specify your SDK key here to authorize your application to connect to a particular environment within LaunchDarkly.
The get() function enforces the singleton pattern. You should only have one instance of the client in your application.
It’s important to make ldclient a singleton for each LaunchDarkly project. The client instance maintains internal state that allows LaunchDarkly to serve feature flags without making any remote requests. Do not instantiate a new client with every request.
If you have multiple LaunchDarkly projects, you can create one LDClient for each. In this situation, the clients operate independently. For example, they do not share a single connection to LaunchDarkly.
Only create one instance of client.
Here’s how:
The Python SDK uses multiple background threads to operate correctly. If the SDK is deployed to an environment which forks from the main process, the SDK may not operate as expected. To learn more about this problem and how to fix it, refer to Considerations with worker-based servers, below.
You can use client to check which variation a particular context will receive for a given feature flag. To learn more, read Evaluating flags and Flag evaluation reasons. For more information about how contexts are specified, read Context configuration.
In the v8.0 example, the context key is the string “example-context-key”. In the v7.x example, the user key is the string “example-user-key”:
The LaunchDarkly SDK relies on multiple threads to operate correctly. These threads provide essential functionality, including delivering flag updates and sending event data.
If the main process which instantiated the SDK is itself forked, the SDK will still evaluate flags, but it will be unable to receive changes to those flags in that child process. This is because threads do not survive the forking process in Python.
The good news is the LaunchDarkly SDK is compatible with worker-based servers. However, there are a few considerations:
postfork() to reinitialize the client. This way, your client will accurately reflect flag changes in the forked thread.Here’s how:
Any configuration that you provide to the SDK must survive the forking process independently. We recommend that you add any listeners or hooks after the postfork() call, unless you are certain they can survive the forking process.
If you are using the Relay Proxy, you must use Relay Proxy v8.11 or later to use postfork().
To learn more about the specific configuration options available in this SDK, read ldclient.config. To learn more about the postfork() reinitialization, read ldclient.postfork.
In uWSGI environments, you must set the enable-threads option. After uwsgi has forked the worker process, you can call postfork() to reinitialize the client. You can do this using uwsgidecorators.
Here’s how:
Python’s standard HTTP library provides a built-in HTTPS proxy. If the HTTPS_PROXY environment variable is present, then the SDK will proxy all network requests through the URL provided.
Here’s how to set the HTTPS_PROXY environment variable on Mac/Linux systems:
Here’s how to set the HTTPS_PROXY environment variable on Windows systems:
Here’s how to set the HTTPS_PROXY environment variable from within Python:
Shut down the client when your application terminates. To learn more, read Shutting down.
This SDK supports the following features: