Python SDK reference
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:
SDK version compatibility
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:
Next, import the LaunchDarkly client in your application code:
The Python SDK uses an SDK key
The Python SDK uses an SDK key. Keys are specific to each project and environment. They are available from Project settings, on the Environments list. 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.
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:
Worker-based servers require specific setup
The Python SDK uses multiple background threads to operate correctly. If the SDK is deployed to an environment which forks from the main process, the SDK may not operate as expected. To learn more about this problem and how to fix it, refer to Considerations with worker-based servers, below.
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”:
Considerations with worker-based servers
The LaunchDarkly SDK relies on multiple threads to operate correctly. These threads provide essential functionality, including delivering flag updates and sending event data.
If the main process which instantiated the SDK is itself forked, the SDK will still evaluate flags, but it will be unable to receive changes to those flags in that child process. This is because threads do not survive the forking process in Python.
The good news is the LaunchDarkly SDK is compatible with worker-based servers. However, there are a few considerations:
- You should initialize a new client before the forking process.
- After you have forked the worker process, you can call
postfork()
to reinitialize the client. This way, your client will accurately reflect flag changes in the forked thread.
Here’s how:
Any configuration that you provide to the SDK must survive the forking process independently. We recommend that you add any listeners or hooks after the postfork()
call, unless you are certain they can survive the forking process.
If you are using the Relay Proxy, you must use Relay Proxy v8.11 or later to use postfork()
.
To learn more about the specific configuration options available in this SDK, read ldclient.config
. To learn more about the postfork()
reinitialization, read ldclient.postfork
.
Example: Configure uWSGI
In uWSGI environments, you must set the enable-threads
option. After uwsgi
has forked the worker process, you can call postfork()
to reinitialize the client. You can do this using uwsgidecorators
.
Here’s how:
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:
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:
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