Python SDK reference
Read time: 5 minutes
Last edited: Jun 14, 2024
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.
LaunchDarkly's SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and sample applications:
Resource | Location |
---|---|
SDK API documentation | SDK API docs |
GitHub repository | python-server-sdk |
Sample applications | Python R |
Published module | PyPI |
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:
pip install launchdarkly-server-sdk
Next, import the LaunchDarkly client in your application code:
import ldclientfrom ldclient.config import Config
The Python SDK uses an SDK key. Keys are specific to each project and environment. They are available from the Environments list for each project. 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.
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
.
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":
from ldclient import Contextcontext = 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 featureelse:# 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.
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.postforkdef 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:
- Anonymous contexts and users
- Big segments
- Configuration, including
- Context configuration
- Evaluating flags
- Flag evaluation reasons
- Flushing events
- Getting all flags
- Hooks
- Identifying and changing contexts
- Logging configuration
- Migrations
- Monitoring SDK status
- Offline mode
- OpenTelemetry
- Private attributes
- Reading flags from a file
- Relay Proxy configuration
- Secure mode
- Sending custom events
- Shutting down
- Storing data
- Subscribing to flag changes
- Test data sources
- Web proxy configuration