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


    Python SDK reference

    Read time: 2 minutes
    Last edited: Sep 27, 2022

    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 API documentationSDK API docs
    GitHub repositorypython-server-sdk
    Sample applicationshello-python-server (Python)
    hello-r (R)
    Published modulePyPI

    Getting started

    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.

    Here's how:

    pip install launchdarkly-server-sdk

    Next, import the LaunchDarkly client in your application code:

    import ldclient
    from 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.

    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. 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.

    Only create one instance of client.

    Here's how:

    client = ldclient.get()

    You can use client to check which variation a particular user will receive for a given feature flag.

    Here's how:

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

    Configuring 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
    def post_fork_client_initialization():
    client = ldclient.get()

    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"

    Shutting down

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

    Supported features

    This SDK supports the following features: