RudderStack Python SDK
RudderStack's Python SDK (`rudder-sdk-python`) is an open-source client for tracking and sending server-side customer event data from Python applications. It acts as an open-source Segment alternative, enabling developers to collect and route event data to various downstream destinations. The library is currently at version 2.1.4 and is actively maintained with regular updates.
Common errors
-
SDK returns success, but events are not visible in the destination.
cause Several potential causes: RudderStack server is in 'degraded' or 'maintenance' mode, incorrect data plane URL or write key, Control Plane is inaccessible, or the specific destination is not enabled or misconfigured in the RudderStack dashboard.fix1. Verify your `rudder_analytics.write_key` and `rudder_analytics.dataPlaneUrl` are correct. 2. Check the status of your RudderStack server/data plane (if self-hosted) or the RudderStack cloud dashboard. 3. Confirm the destination is enabled and configured correctly in your RudderStack workspace. 4. Enable `rudder_analytics.debug = True` for more verbose logging. -
HTTPSConnectionPool(...): Max retries exceeded with url: ... (Caused by ProxyError or ConnectionError)
cause Network connectivity issues preventing the SDK from reaching the RudderStack data plane, potentially due to firewall rules, proxy misconfiguration, or an unavailable data plane server. While this specific error often appears with `rudder-cli`, similar underlying network problems can affect the SDK.fix1. Check your network connection and proxy settings. 2. Ensure the `DATA_PLANE_URL` is accessible from your application's environment. 3. Verify that any firewalls are configured to allow outbound HTTP/HTTPS connections to your data plane URL. 4. Increase the `timeout` configuration option for the SDK if transient network issues are expected. -
Events are being sent successfully but appear in the destination with significant delay.
cause This usually indicates a bottleneck in processing, either on the RudderStack backend side or the destination service itself. Possible reasons include a high rate of incoming events overwhelming the system or a temporary outage/degradation of the destination service.fix1. Check the status of your destination service (e.g., Google Analytics, S3). 2. If self-hosting RudderStack, monitor your server resources and consider scaling up if the incoming event rate consistently exceeds processing capacity. 3. Review RudderStack dashboard metrics for any alerts on pending events or degraded performance.
Warnings
- gotcha The Python SDK does not persist user state. You must explicitly provide either `user_id` or `anonymous_id` with every event API call (e.g., `track`, `identify`, `page`). Failing to do so will result in events not being processed correctly or being dropped.
- gotcha The SDK blocks the calling thread until all messages are flushed from the queue. Avoid using SDK calls directly within the critical path of a request lifecycle (e.g., in web server request handlers) to prevent performance bottlenecks.
- gotcha The Python SDK drops any event data larger than 32KB without logging a warning. Ensure your event payloads (including properties and context) do not exceed this size limit.
Install
-
pip install rudder-sdk-python
Imports
- rudderstack.analytics
import rudderstack.analytics
import rudderstack.analytics as rudder_analytics
Quickstart
import os
import rudderstack.analytics as rudder_analytics
# Set your RudderStack Write Key and Data Plane URL from environment variables
# Replace with your actual values or use environment variables for production
WRITE_KEY = os.environ.get('RUDDERSTACK_WRITE_KEY', 'YOUR_WRITE_KEY')
DATA_PLANE_URL = os.environ.get('RUDDERSTACK_DATA_PLANE_URL', 'YOUR_DATA_PLANE_URL')
rudder_analytics.write_key = WRITE_KEY
rudder_analytics.dataPlaneUrl = DATA_PLANE_URL
# Optional: Configure debug mode for verbose logging
rudder_analytics.debug = True
# Identify a user
rudder_analytics.identify(
user_id='user_123',
traits={'name': 'John Doe', 'email': 'john.doe@example.com'}
)
# Track an event
rudder_analytics.track(
user_id='user_123',
event='Product Viewed',
properties={'product_id': '456', 'product_name': 'Example Widget'}
)
# You can also use anonymous_id if user_id is not available
rudder_analytics.page(
anonymous_id='anon_789',
name='Homepage',
category='Marketing'
)
# Ensure all queued events are sent before exiting
rudder_analytics.flush()
print('Events sent to RudderStack.')