• HOME
  • INTEGRATIONS
  • SDKS
  • GUIDES
  • API DOCS
No results for ""
EXPAND ALL

EDIT ON GITHUB

Python SDK reference

Read time: 2 minutes
Last edited: Jul 28, 2021

This reference guide documents all of the methods available in the Python SDK, and explains in detail how these methods work. If you want to dig even deeper, our 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. Additionally you can clone and run a sample application using this SDK. For an example of using the Python SDK inside an R application see hello-r.

Getting started

Building on top of our Getting Started guide, the following steps will get you started with 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:

1ldclient.set_config(Config("YOUR_SDK_KEY"))
2ld_client = ldclient.get()

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

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

Lastly, when your application is about to terminate, shut down ld_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
2ld_client.close()

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: