Python SDK reference
Read time: 4 minutes
Last edited: Jul 26, 2023
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."
Code samples on this page are from the two most recent SDK versions where they differ. To learn more about upgrading, read Python SDK 7.x to 8.0 migration guide.
This topic documents how to get started with the Python SDK, and links to reference information on all of the supported features.
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.
pip install launchdarkly-server-sdk
Next, import the LaunchDarkly client in your application code:
import ldclientfrom ldclient.config import Config
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.
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
ldclient.set_config(Config("sdk-key-123abc"))client = ldclient.get()
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 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
The LaunchDarkly SDK is compatible with uWSGI. However, there are a few considerations.
First, in uWSGI environments, you must set the
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 using
import firstname.lastname@example.org post_fork_client_initialization():ldclient.set_config(LDConfig("sdk-key-123abc"))client = ldclient.get()end
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:
Here's how to set the HTTPS_PROXY environment variable on Windows systems:
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 when your application terminates. To learn more, read Shutting down.
This SDK supports the following features:
- Anonymous contexts and users
- Big Segments
- Configuration, including
- Evaluating flags
- Flag evaluation reasons
- Flushing events
- Getting all flags
- Identifying and changing contexts
- Logging configuration
- Offline mode
- Private attributes
- Reading flags from a file
- Relay Proxy configuration
- Secure mode
- Sending custom events
- Shutting down
- Storing data
- Test data sources
- User and context configuration
- Web proxy configuration