Set up Python SDK

Install the package

pip
$pip3 install launchdarkly-server-sdk

Initialize the SDK

Python SDK initialization
1import os
2import ldclient
3from ldclient import Context
4from ldclient.config import Config
5
6if __name__ == '__main__':
7 # Set your LaunchDarkly SDK key.
8 # This is inlined as example only for onboarding.
9 # Never hardcode your SDK key in production.
10 ldclient.set_config(Config('YOUR_SDK_KEY'))
11
12 if not ldclient.get().is_initialized():
13 print('SDK failed to initialize')
14 exit()
15
16 # A "context" is a data object representing users, devices, organizations, and
17 # other entities. You'll need this later, but you can ignore it for now.
18 context = (
19 Context.builder('EXAMPLE_CONTEXT_KEY')
20 .kind('user')
21 .set('email', 'biz@face.dev')
22 .build()
23 )
24
25 # For onboarding purposes only we flush events as soon as
26 # possible so we quickly detect your connection.
27 # You don't have to do this in practice because events are automatically flushed.
28 ldclient.get().flush()
29 print('SDK successfully initialized')

You can find your server-side SDK key, client-side ID, and mobile key in the “Resources” section of the help menu. Click the help icon at the bottom left corner of the LaunchDarkly UI, then choose SDK keys:

The SDK keys option in the help menu.

The SDK keys option in the help menu.

To learn more, read Initialize the client in the Python SDK reference guide.