> ## Documentation Index
> Fetch the complete documentation index at: https://launchdarkly-preview.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Python SDK reference

> This topic documents how to get started with the Python SDK, and links to reference information on all of the supported features.

> For the complete documentation index, see [llms.txt](/llms.txt). For the full documentation snapshot, see [llms-full.txt](/llms-full.txt).

## Overview

This topic documents how to get started with the Python SDK, and links to reference information on all of the supported features.

<Note title="SDK quick links">
  LaunchDarkly's SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and sample applications:
</Note>

| Resource | Location |
| - | - |
| SDK API documentation | [SDK API docs](https://launchdarkly-python-sdk.readthedocs.io/) |
| Supported SDK Versions | [Python server SDK](../concepts/supported-versions#python-server-sdk) |
| GitHub repository | [python-server-sdk](https://github.com/launchdarkly/python-server-sdk) |
| Sample applications | [Python](https://github.com/launchdarkly/hello-python) ,  [R](https://github.com/launchdarkly/hello-r) |
| Published module | [PyPI](https://pypi.org/project/launchdarkly-server-sdk/) |

<Note title="SDK version compatibility">
  The LaunchDarkly Python SDK, version 9.0 through 9.11, is compatible with Python 3.8.0 and higher. Starting with version 9.12, the SDK requires Python 3.9.0 or higher.
</Note>

## Get started

After you complete the [Getting Started process](/home/getting-started), 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](https://github.com/launchdarkly/python-server-sdk/releases) to identify the latest version.

We recommend making the LaunchDarkly [observability plugin](/sdk/observability) available as well. This plugin collects and sends observability data to LaunchDarkly, including [metrics autogenerated from OpenTelemetry data](/home/metrics/autogen/opentelemetry). This means you can review error monitoring, logs, and traces from within the LaunchDarkly UI. They require the Python SDK version 9.12 or later.

Here's how:

<CodeGroup>
  ```bash title="Shell" theme={null}
      pip install launchdarkly-server-sdk

      # optional observability plugin, requires Python SDK v9.12+
      pip install launchdarkly-observability
  ```
</CodeGroup>

Next, import the LaunchDarkly client in your application code:

<CodeGroup>
  ```python title="Python" theme={null}
      import ldclient
      from ldclient.config import Config

      # optional observability plugin, requires Python SDK v9.12+
      import ldobserve
      from ldobserve import ObservabilityConfig, ObservabilityPlugin, observe
  ```
</CodeGroup>

<Note title="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](/sdk/concepts/client-side-server-side#keys).
</Note>

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

<Warning title="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.
</Warning>

Only create one instance of `client`.

Here's how:

<CodeGroup>
  ```python title="Python" theme={null}
      ldclient.set_config(Config("YOUR_SDK_KEY",
        # optional observability plugin, requires Python SDK v9.12+
        plugins=[ObservabilityPlugin()]
        )
      )
      client = ldclient.get()
  ```
</CodeGroup>

<Warning title="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](#considerations-with-worker-based-servers), below.
</Warning>

### 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](/sdk/features/evaluating) and [Flag evaluation reasons](/sdk/features/evaluation-reasons). For more information about how contexts are specified, read [Context configuration](/sdk/features/context-config).

In the v8.0 example, the context key is the string "example-context-key". In the v7.x example, the user key is the string "example-user-key":

<CodeGroup>
  ```python title="Python SDK v8.0+" theme={null}
      from ldclient import Context

      context = Context.builder("example-context-key").name("Sandy").build()
      flag_value = client.variation("example-flag-key", context, False)

      if flag_value:
          # application code to show the feature
      else:
          # the code to run if the feature is off
  ```
</CodeGroup>

## 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:

<CodeGroup>
  ```python title="Python SDK v9.11+" theme={null}
      # 1. Create the client before forking.
      ldclient.set_config(Config("YOUR_SDK_KEY"))
      client = ldclient.get()

      # 2. From the newly forked process, reinitialize the client by calling `postfork`.
      # Examples for specific servers are shown below.
      client.postfork()
  ```
</CodeGroup>

Any configuration that you provide to the SDK must survive the forking process independently. We recommend that you add any [listeners](/sdk/features/monitoring#python) or [hooks](/sdk/features/hooks#python) after the `postfork()` call, unless you are certain they can survive the forking process.

If you are using the [Relay Proxy](/sdk/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`](https://launchdarkly-python-sdk.readthedocs.io/en/latest/api-main.html#module-ldclient.config). To learn more about the `postfork()` reinitialization, read [`ldclient.postfork`](https://launchdarkly-python-sdk.readthedocs.io/en/latest/api-main.html#ldclient.client.LDClient.postfork).

### Example: Configure u

WSGI

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:

<CodeGroup>
  ```python title="Python uWSGI initialization" theme={null}
      import uwsgidecorators

      # Instantiate the client before the fork
      ldclient.set_config(LDConfig("YOUR_SDK_KEY"))
      client = ldclient.get()

      @uwsgidecorators.postfork
      def post_fork_client_initialization():
          # Reinitialize the client after the fork
          ldclient.get().postfork()

  ```
</CodeGroup>

## 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:

<CodeGroup>
  ```bash title="Shell" theme={null}
      export HTTPS_PROXY=https://web-proxy.domain.com:8080
  ```
</CodeGroup>

Here's how to set the HTTPS\_PROXY environment variable on Windows systems:

<CodeGroup>
  ```bash title="cmd" theme={null}
      set HTTPS_PROXY=https://web-proxy.domain.com:8080
  ```
</CodeGroup>

Here's how to set the HTTPS\_PROXY environment variable from within Python:

<CodeGroup>
  ```python title="Python" theme={null}
      os.environ["HTTPS_PROXY"] = "https://web-proxy.domain.com:8080"
  ```
</CodeGroup>

## Shut down the client

Shut down the client when your application terminates. To learn more, read [Shutting down](/sdk/features/shutdown#python).

## Supported features

This SDK supports the following features:

* [Anonymous contexts and users](/sdk/features/anonymous#python)
* [Big segments](/sdk/features/big-segments#python)
* [Configuration](/sdk/features/config#python), including
  * [Application metadata configuration](/sdk/features/app-config#python)
  * [Migration configuration](/sdk/features/migration-config#python)
  * [Service endpoint configuration](/sdk/features/service-endpoint-configuration/server-side#python)
* [Context configuration](/sdk/features/context-config/server-side#python)
* [Data saving mode](/sdk/features/data-saving-mode#python)
* [Evaluating flags](/sdk/features/evaluating#python)
* [Flag evaluation reasons](/sdk/features/evaluation-reasons/server-side#python)
* [Flushing events](/sdk/features/flush#python)
* [Getting all flags](/sdk/features/all-flags#python)
* [Hooks](/sdk/features/hooks#python)
* [Identifying and changing contexts](/sdk/features/identify#python)
* [Logging configuration](/sdk/features/logging#python)
* [Migrations](/sdk/features/migrations#python)
* [Monitoring SDK status](/sdk/features/monitoring#python)
* [Observability](/sdk/observability/python)
* [Offline mode](/sdk/features/offline-mode#python)
* [OpenTelemetry](/sdk/features/opentelemetry-server-side#python)
* [Private attributes](/sdk/features/private-attributes/server-side#python)
* [Reading flags from a file](/sdk/features/flags-from-files#python)
* [Relay Proxy configuration](/sdk/features/relay-proxy-configuration)
  * [Using proxy mode](/sdk/features/relay-proxy-configuration/proxy-mode#python)
  * [Using daemon mode](/sdk/features/relay-proxy-configuration/daemon-mode#python)
* [Secure mode](/sdk/features/secure-mode#python)
* [Sending custom events](/sdk/features/events#python)
* [Shutting down](/sdk/features/shutdown#python)
* [Storing data](/sdk/features/storing-data#python)
* [Subscribing to flag changes](/sdk/features/flag-changes#python)
* [Test data sources](/sdk/features/test-data-sources#python)
* [Web proxy configuration](/sdk/features/web-proxy#python)
