{"id":2963,"library":"growthbook","title":"GrowthBook Python SDK","description":"GrowthBook is a Python SDK for powerful feature flagging and A/B testing in Python applications. It enables local evaluation of feature flags and experiments, flexible targeting, and integration with existing event tracking systems. The library is lightweight, fast, and supports both synchronous and asynchronous usage. As of version 2.2.0, it is actively maintained with frequent releases.","status":"active","version":"2.2.0","language":"en","source_language":"en","source_url":"https://github.com/growthbook/growthbook-python","tags":["feature flags","a/b testing","experimentation","server-side","client-side"],"install":[{"cmd":"pip install growthbook","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"symbol":"GrowthBook","correct":"from growthbook import GrowthBook"},{"note":"Recommended for async applications for improved performance and resource utilization.","symbol":"GrowthBookClient","correct":"from growthbook import GrowthBookClient"}],"quickstart":{"code":"import asyncio\nimport os\nfrom growthbook import GrowthBookClient, Options, UserContext, FeatureRefreshStrategy\n\nasync def main():\n    # Get client key from environment variable for security\n    client_key = os.environ.get(\"GROWTHBOOK_CLIENT_KEY\", \"sdk-YOUR_CLIENT_KEY\")\n    api_host = os.environ.get(\"GROWTHBOOK_API_HOST\", \"https://cdn.growthbook.io\")\n\n    # User attributes for targeting and experimentation\n    attributes = {\n        \"id\": \"user-123\",\n        \"country\": \"US\",\n        \"loggedIn\": True\n    }\n\n    # Configure GrowthBook client options\n    options = Options(\n        api_host=api_host,\n        client_key=client_key,\n        # Optional: Enable real-time feature updates via Server-Sent Events (SSE)\n        refresh_strategy=FeatureRefreshStrategy.SERVER_SENT_EVENTS\n    )\n\n    # Create a UserContext instance\n    user_context = UserContext(attributes=attributes)\n\n    # Create an async GrowthBookClient instance (recommended for async apps)\n    async with GrowthBookClient(options=options, user_context=user_context) as gb:\n        # Load features from the GrowthBook API\n        await gb.load_features()\n\n        # Simple on/off feature gating\n        if gb.is_on(\"my-awesome-feature\"): # Replace with your feature key\n            print(\"My awesome feature is ON!\")\n        else:\n            print(\"My awesome feature is OFF.\")\n\n        # Get the value of a feature with a fallback\n        button_color = gb.get_feature_value(\"button-color-feature\", \"blue\") # Replace with your feature key\n        print(f\"Button color feature value: {button_color}\")\n\n        # Example for an experiment\n        # Make sure 'pricing-experiment' and variations are configured in GrowthBook\n        pricing_result = gb.run(\n            {\n                \"key\": \"pricing-experiment\",\n                \"variations\": [\"control\", \"variant-a\", \"variant-b\"]\n            }\n        )\n\n        if pricing_result.in_experiment:\n            print(f\"User in pricing experiment, assigned variation: {pricing_result.value}\")\n        else:\n            print(\"User not in pricing experiment.\")\n\n    print(\"GrowthBook client closed.\")\n\nif __name__ == \"__main__\":\n    # Set dummy keys for local testing, replace with actual keys or environment variables\n    os.environ[\"GROWTHBOOK_CLIENT_KEY\"] = \"sdk-example12345\"\n    os.environ[\"GROWTHBOOK_API_HOST\"] = \"https://cdn.growthbook.io\"\n    asyncio.run(main())\n","lang":"python","description":"This quickstart demonstrates how to initialize and use the `GrowthBookClient` for asynchronous applications, fetch feature flags, check feature status, retrieve feature values, and run A/B experiments. It uses environment variables for `client_key` and `api_host` for secure and flexible configuration. The `GrowthBookClient` is recommended for performance and resource utilization in async environments."},"warnings":[{"fix":"Migrate `on_experiment_viewed` or `set_event_logger` callbacks to be synchronous, or wrap them with your own asynchronous handling mechanism if needed.","message":"Version 2.0.0 removed async tracking callback executions, enforcing synchronous execution for reliability. If you previously relied on async callbacks, you will need to adjust your implementation to handle tracking synchronously or implement your own async wrapper.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Hash sensitive attributes using `hashlib.sha256(attribute_value + secure_salt).hexdigest()` before including them in the `attributes` dictionary.","message":"If 'Secure Attributes' are enabled in your GrowthBook SDK Connection, you must manually hash any `secureString` or `secureString[]` attributes using SHA-256 with your organization's secure attribute salt before passing them to the SDK.","severity":"gotcha","affected_versions":"All"},{"fix":"Implement and pass an `on_experiment_viewed` callable to the `GrowthBook` constructor or use `gb.set_event_logger` to record experiment view events to your analytics system.","message":"To analyze experiment results in the GrowthBook platform, you must provide an `on_experiment_viewed` callback (for `GrowthBook` class) or configure an event logger via `set_event_logger` (for `GrowthBook` or `GrowthBookClient` from v2.2.0+). Without this callback, feature flags will work, but experiment assignments will not be tracked.","severity":"gotcha","affected_versions":"All"},{"fix":"Use `GrowthBookClient` and its `async with` context manager for async applications, and `await gb.load_features()` for fetching data.","message":"For improved performance and better resource utilization, especially in async web applications (e.g., FastAPI, Django Async), the `GrowthBookClient` class is recommended over the synchronous `GrowthBook` class. It's designed to reuse a single client instance.","severity":"gotcha","affected_versions":">=1.2.0"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}