Python error tracking installation
Install the PostHog Python library using pip: Initialize the PostHog client with your project API key and host from your project settings: If you're using Django, check out our Django integration for automatic request tracking. Once installed, PostHog will automatically start capturing events. You can also manually send events to test your integration: Capture custom events by calling the Before proceeding, enable debug and call Exception autocapture can be enabled during initialization of the PostHog client to automatically capture any unhandled exceptions thrown by your Python application. It works by setting Python's built-in exception hooks, such as We recommend setting up and using contexts so that exceptions automatically include distinct IDs, session IDs, and other properties you can set up with tags. You can also enable code variables capture to automatically capture the state of local variables when exceptions occur, giving you a debugger-like view of your application. For exceptions handled by your application that you would still like sent to PostHog, you can manually call the capture method: You can find a full example of all of this in our Python (and Flask) error tracking tutorial. Python frameworks often have built-in error handlers. This means PostHog's default exception autocapture won't work and we need to manually capture errors instead. The exact process depends on the framework: The Python SDK provides a Django middleware that automatically wraps all requests with a context. Add the middleware to your Django settings: By default, the middleware captures exceptions and sends them to PostHog. Disable with Install the package
RequiredInitialize PostHog
RequiredSend events
Recommendedcapture method with an event name and properties:Verify PostHog is initialized
Recommendedposthog.capture('test_event') to make sure you can capture events.Setting up exception autocapture
Recommendedsys.excepthook and threading.excepthook.Manually capturing exceptions
OptionalFramework-specific exception capture
OptionalPOSTHOG_MW_CAPTURE_EXCEPTIONS = False. Use POSTHOG_MW_EXTRA_TAGS, POSTHOG_MW_REQUEST_FILTER, and POSTHOG_MW_TAG_MAP to customize. See the Django integration docs for full configuration.

