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

    EDIT ON GITHUB

    Python SDK reference

    Read time: 2 minutes
    Last edited: Jan 20, 2023
    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.

    You can upgrade your SDK at any time. However, the ability to target by context, or review context instances that have encountered flags in your application, is only available for customers in the contexts Early Access Program (EAP). If you want access this feature, join the EAP. If you are not part of the EAP, your application can send user contexts to LaunchDarkly and they will appear on the Users list.

    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.

    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:

    ResourceLocation
    SDK API documentationSDK API docs
    GitHub repositorypython-server-sdk
    Sample applicationsPython
    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 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()

    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 Context
    context = 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 feature
    else:
    # 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
    @uwsgidecorators.postfork
    def 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"

    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: