• Home
  • Integrations
  • SDKs
  • Guides
  • API docs
No results for ""


Python SDK reference

Read time: 2 minutes
Last edited: Oct 22, 2021

This reference guide documents all of the methods available in the Python SDK, and explains in detail how these methods work. LaunchDarkly's SDKs are open source. To learn more, read Python SDK GitHub repository. The online Python API docs contain the programmatic definitions of every class and method. You can also try this SDK out by cloning and running a sample application. For an example of using the Python SDK inside an R application see hello-r.

Getting started

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

The first step is to install the LaunchDarkly SDK as a dependency in your application using your application's dependency manager. Refer to the SDK releases page to identify the latest version if you want to depend on a specific version.

Here's how:

1pip install launchdarkly-server-sdk

Next you should import the LaunchDarkly client in your application code:

1import ldclient
2from ldclient.config import Config

Once the SDK is installed and imported, you'll want to create a single, shared instance of ldclient. The get() function enforces the singleton pattern; you should only have one instance of the client in your application. You should specify your SDK key here so that your application will be authorized to connect to LaunchDarkly and for your application and environment.

To create a single instance:

2client = ldclient.get()

Using client, you can check which variation a particular user should receive for a given feature flag:

1show_feature = client.variation("your.flag.key", {"key": "user@test.com"}, False)
3if show_feature:
4 # application code to show the feature
6 # the code to run if the feature is off

Lastly, when your application is about to terminate, shut down client. This ensures that the client releases any resources it is using, and that any pending analytics events are delivered to LaunchDarkly. If your application quits without this shutdown step, you may not see your requests and users on the dashboard, because they are derived from analytics events. This is something you only need to do once.

Here's how:

1# shut down the client, since we're about to quit

Configuring uWSGI

The LaunchDarkly SDK is compatible with uWSGI. However, in uWSGI environments, the SDK requires the enable-threads option to be set.

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.

How to set the HTTPS_PROXY environment variable on Mac/Linux systems:

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

How to set the HTTPS_PROXY environment variable on Windows systems:

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

Or it can be set from within Python:

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

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

Supported features

This SDK supports the following features: