No results for ""
EXPAND ALL
  • Home
  • API docs

GIVE DOCS FEEDBACK

Python SDK reference

Read time: 5 minutes
Last edited: Mar 04, 2024
Recent major versions

Version 9 of the Python SDK supports migration feature flags. These are temporary flags used to migrate data or systems while keeping your application available and disruption free. To learn more about upgrading, read Python SDK 8.x to 9.0 migration guide.

Version 8 of the Python SDK replaces users with contexts. A context is a generalized way of referring to the people, services, machines, or other resources that encounter feature flags in your product. Contexts replace another data object in LaunchDarkly: "users." To learn more about upgrading, read Python SDK 7.x to 8.0 migration guide.

Code samples on this page are from the three most recent SDK versions where they differ.

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.

Getting started

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.

Here's how:

pip install launchdarkly-server-sdk

Next, import the LaunchDarkly client in your application code:

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

The Python SDK uses an SDK key. Your environment's SDK key is available in the Projects tab of your Account settings page. 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.

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:

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

To learn more about the specific configuration options available in this SDK, read ldclient.config.

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 User and 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":

from ldclient import Context
context = Context.builder("context-key-123abc").name("Sandy").build()
flag_value = client.variation("flag-key-123abc", context, False)
if flag_value:
# application code to show the feature
else:
# the code to run if the feature is off

Configuring 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.

Additionally, you should initialize a new client only after uwsgi has forked the worker process. This way your client will accurately reflect flag changes in the forked thread. You can do this usinguwsgidecorators.

Here's how:

import uwsgidecorators
@uwsgidecorators.postfork
def post_fork_client_initialization():
ldclient.set_config(LDConfig("sdk-key-123abc"))
client = ldclient.get()
end

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:

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

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

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

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

os.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: