Python SDK reference

Overview

This topic documents how to get started with the Python SDK, and links to reference information on all of the supported features.

SDK quick links

LaunchDarkly’s SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and sample applications:

ResourceLocation
SDK API documentationSDK API docs
GitHub repositorypython-server-sdk
Sample applicationsPython
R
Published modulePyPI
SDK version compatibility

The LaunchDarkly Python SDK, version 9.0 and higher, is compatible with Python 3.8.0 and higher.

Get started

After you complete the Getting Started process, follow these instructions to start using the LaunchDarkly SDK in your Python application.

Install the SDK

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.

Here’s how:

Shell
$pip install launchdarkly-server-sdk

Next, import the LaunchDarkly client in your application code:

Python
1import ldclient
2from ldclient.config import Config
The Python SDK uses an SDK key

The Python SDK uses an SDK key. Keys are specific to each project and environment. They are available from Project settings, on the Environments list. To learn more about key types, read Keys.

Initialize the client

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.

ldclient must be a singleton

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:

Python
1ldclient.set_config(Config("sdk-key-123abc"))
2client = ldclient.get()

The Python SDK relies on multiple background threads to operate correctly. When a process forks, the SDK will not function correctly in the child process until it is reinitialized. Use the postfork() method to reinitialize an existing client after a process fork.

Here’s how:

Python SDK v9.11+
1ldclient.set_config(Config("sdk-key-123abc"))
2client = ldclient.get()
3
4# From the child process, reinitialize the client by calling `postfork`.
5client = ldclient.postfork()

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.

Evaluate a context

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 “context-key-123abc”. In the v7.x example, the user key is the string “user-key-123abc”:

Python SDK v8.0+
1from ldclient import Context
2
3context = Context.builder("context-key-123abc").name("Sandy").build()
4flag_value = client.variation("flag-key-123abc", context, False)
5
6if flag_value:
7 # application code to show the feature
8else:
9 # the code to run if the feature is off

Configure uWSGI

The LaunchDarkly SDK is compatible with uWSGI. However, there are a few considerations.

First, in uWSGI environments, you must set the enable-threads option.

You should initialize a new client before the forking process. Once uwsgi has forked the worker process, you can call postfork() to reinitialize the client. This way, your client will accurately reflect flag changes in the forked thread.

You can do this usinguwsgidecorators.

Here’s how:

Python uWSGI initialization
1import uwsgidecorators
2
3# Instantiate the client before the fork
4ldclient.set_config(LDConfig("sdk-key-123abc"))
5client = ldclient.get()
6
7@uwsgidecorators.postfork
8def post_fork_client_initialization():
9 # Reinitialize the client after the fork
10 ldclient.get().postfork()

HTTPS proxy

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:

Shell
$export HTTPS_PROXY=https://web-proxy.domain.com:8080

Here’s how to set the HTTPS_PROXY environment variable on Windows systems:

cmd
$set HTTPS_PROXY=https://web-proxy.domain.com:8080

Here’s how to set the HTTPS_PROXY environment variable from within Python:

Python
1os.environ["HTTPS_PROXY"] = "https://web-proxy.domain.com:8080"

Shut down the client

Shut down the client when your application terminates. To learn more, read Shutting down.

Supported features

This SDK supports the following features: