Python Error Tracking installation
Contents
Install with the wizard
Run this command in your project directory to add PostHog to your Python project. The wizard detects your framework, installs the SDK, and sets up your first events.
Or set it up manually by following the rest of this guide.
- 1
Install the package
RequiredInstall the PostHog Python library using pip:
Terminal - 2
Initialize PostHog
RequiredInitialize the PostHog client with your project token and host from your project settings:
PythonDjango integrationIf you're using Django, check out our Django integration for automatic request tracking.
- 3
Send events
RecommendedOnce installed, PostHog will automatically start capturing events. You can also manually send events to test your integration:
Capture custom events by calling the
capturemethod with an event name and properties:Python Verify PostHog is initialized
RecommendedBefore proceeding, enable debug and call
posthog.capture('test_event')to make sure you can capture events.- 4
Setting up exception autocapture
RecommendedException 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
sys.excepthookandthreading.excepthook.PythonWe 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.
- 5
Manually capturing exceptions
OptionalFor exceptions handled by your application that you would still like sent to PostHog, you can manually call the capture method:
PythonYou can find a full example of all of this in our Python (and Flask) error tracking tutorial.
- 6
Framework-specific exception capture
RecommendedWeb frameworks catch exceptions raised in your routes and turn them into error responses, so these exceptions never reach the hooks that exception autocapture uses. The Python SDK includes integrations for Django, Flask, and ASGI frameworks such as FastAPI. They capture these exceptions and add request properties to every event captured during a request. The Flask, ASGI, and Django REST Framework integrations require version
7.69.0or later of the Python SDK.Add the Django middleware to your settings. It wraps each request in a context and captures exceptions raised by your views.
The middleware sends exceptions through the global
posthogclient. When you create your own client withPosthog(...), set it asPOSTHOG_MW_CLIENT. Otherwise the middleware sends exceptions to the unconfigured global client, and they don't reach PostHog:settings.pyIf you configure the global client instead, with
posthog.api_keyandposthog.host, leavePOSTHOG_MW_CLIENTunset.Django REST Framework converts exceptions into error responses before the middleware sees them. If you use it, also add the DRF exception handler:
settings.pySet
POSTHOG_MW_CAPTURE_EXCEPTIONS = Falseto keep the request context without exception capture. See the Django integration docs for all settings.Register the Flask integration with your app and your client. It captures unhandled exceptions raised by your routes:
PythonThe integration doesn't capture expected HTTP exceptions, such as
404 Not Found, or exceptions that your own error handlers consume. To report an exception that you handle, callposthog.capture_exception(e)in the handler. See the Flask integration docs.Add the ASGI middleware to your app. It captures exceptions that escape your routes. It works the same way for Starlette, Litestar, and other ASGI frameworks:
PythonThe middleware still captures an exception that an
@app.exception_handler(Exception)catch-all handles. A handler for a specific exception class, such as@app.exception_handler(ValueError), turns that exception into a response before the middleware sees it. To report those exceptions, callposthog.capture_exception(exc)in the handler. See the ASGI middleware docs. - 7
Web framework integrations
The Python SDK includes request context and error tracking integrations for common web frameworks:













