Registry / communication / posthog

posthog

JSON →
library7.44.2pypypi✓ verified 27d ago

The PostHog Python SDK makes it easy to capture events, evaluate feature flags, track errors, and more in your Python applications. It supports event tracking, user identification, group analytics, and local evaluation for feature flags. The library is actively maintained, with frequent updates, and the current version is 7.9.12.

pip install posthog
INSTALL
IMPORT
SIG · POSTHOG
P
posthog
communicationpythonv7.44.2
Install
2.7s avg
Import
588ms
Disk
25MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v7.44.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.614s · 26.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.7s · import 0.562s · 27MB
25MB installed
● package 25MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Posthog
from posthog import Posthog
Initializes the main PostHog client.
capture
posthog.capture(...)
Called on an initialized Posthog client instance.
identify_context
from posthog import identify_context
Used with contexts for user identification, especially in server-side applications.
new_context
from posthog import new_context
Used to create a new scope for managing shared state across capture calls.

This quickstart demonstrates how to initialize the PostHog client with environment variables, capture a custom event, and evaluate a feature flag. It also includes an important step for graceful shutdown to ensure all buffered events are sent.

import os from posthog import Posthog # Initialize PostHog client using environment variables for API key and host # Replace 'your_project_api_key' and 'https://us.i.posthog.com' with your actual values # or ensure POSTHOG_PROJECT_API_KEY and POSTHOG_HOST are set in your environment. posthog = Posthog( project_api_key=os.environ.get('POSTHOG_PROJECT_API_KEY', 'your_project_api_key'), host=os.environ.get('POSTHOG_HOST', 'https://us.i.posthog.com') ) # Capture a custom event distinct_id = "user_123_unique_id" # A unique identifier for the user or entity posthog.capture( distinct_id=distinct_id, event="user_signed_up", properties={ "plan": "free", "signup_method": "email_password" } ) # Evaluate a feature flag is_new_feature_enabled = posthog.feature_enabled( 'new-feature-flag-key', distinct_id # Feature flags usually require a distinct_id ) if is_new_feature_enabled: print(f"New feature is enabled for {distinct_id}!") else: print(f"New feature is NOT enabled for {distinct_id}.") # In serverless environments or scripts, ensure events are flushed before exiting. # For long-running applications (e.g., web servers), this is often handled automatically # by background threads, but explicit shutdown is good practice for reliability. posthog.shutdown() print("PostHog client shut down and events flushed.")
Debug
Known issues
breakingVersion 6.x introduced breaking changes, most notably deprecating the global `posthog.identify()` method in favor of a new `contexts` API. The `capture()` method signature also changed, requiring keyword arguments instead of positional ones for `distinct_id`.
fix
Migrate from `identify()` to `identify_context()` within a `new_context()` block. Update all `capture()` and `capture_exception()` calls to use keyword arguments for parameters like `distinct_id` and `event`.
affects: 6.x.x onwards
breakingPostHog Python SDK versions 7.x.x and higher no longer support Python 3.9. Projects using Python 3.9 will need to upgrade their Python version to 3.10 or newer to use the latest SDK versions.
fix
Upgrade your Python environment to 3.10 or higher before upgrading to PostHog SDK version 7.0.0+.
affects: 7.0.0 onwards
gotchaIn serverless environments (e.g., AWS Lambda, Google Cloud Functions), buffered events might be lost if the function terminates before the PostHog client can flush its queue. By default, events are sent on background threads.
fix
Ensure `posthog.shutdown()` is explicitly called before the function exits, or initialize the client with `sync_mode=True` (e.g., `posthog = Posthog(..., sync_mode=True)`) to send events synchronously.
affects: All versions
gotchaThe primary `posthog` client is designed with background threads for non-blocking `capture()` calls, but it is not inherently `async/await` compatible. Developers expecting full `asyncio` integration might find this limiting.
fix
For true asynchronous use cases, consider existing asynchronous forks (e.g., `posthog-async` - note this is a separate package) or specific async integrations provided by PostHog (e.g., for LLM clients). The core client's `capture()` is non-blocking but not `awaitable`.
affects: All versions
gotchaPostHog JavaScript snippets can be blocked by ad blockers in browsers, potentially leading to incomplete data. While this is a frontend concern, it can impact the comprehensiveness of analytics data if not addressed.
fix
For production environments, consider setting up a reverse proxy to circumvent ad blockers and ensure more reliable data ingestion.
affects: All versions
gotchaHardcoding your PostHog `project_api_key` and `host` directly in your application code is a security risk, especially for production deployments.
fix
Always use environment variables (e.g., `os.environ.get('POSTHOG_PROJECT_API_KEY')`) to securely pass your API key and host to the PostHog client upon initialization.
affects: All versions
gotchaThe PostHog client will raise a `posthog.request.APIError` (with a 401 status code) if the `project_api_key` provided upon initialization is invalid or has expired, preventing events from being sent or feature flags from being fetched.
fix
Ensure the `project_api_key` used to initialize the PostHog client is correct and active for your PostHog project. Verify it against your project settings in PostHog. Double-check for typos and ensure your `host` configuration is also correct.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'posthog'
The `posthog` Python package has not been installed in the current Python environment.
fix
Install the PostHog Python library using pip: `pip install posthog`
ImportError: cannot import name 'Posthog' from partially initialized module 'posthog' (most likely due to a circular import)
This error commonly occurs when you have a local Python file or directory named `posthog.py` or `posthog` within your project's import path, which conflicts with the installed `posthog` library.
fix
Rename your local file or directory (e.g., `posthog.py` to `my_posthog_utils.py`) to avoid the name collision with the official PostHog package.
401 Unauthorized
This HTTP status code indicates that the API key provided during the PostHog client initialization is incorrect, missing, or unauthorized, preventing events from being accepted by the PostHog instance.
fix
Verify that the `api_key` and `host` (if self-hosting) passed to `posthog.init()` are correct and match your PostHog project settings. Ensure environment variables are loaded correctly if used.
Upgrade
Version history
7.44.2latest on PyPI · released Aug 27, 2026
Audit
Dependencies
pythonrequiredRequires Python 3.10 or higher for SDK versions 7.x.x+.
Agent activity
27 hits · last 30 days
node
20
OpenAI (training)
2
Resources
posthog — pip install posthog · libregistry