{"id":1558,"library":"mixpanel","title":"Mixpanel Python SDK","description":"The official Mixpanel library for Python provides functionalities to track events, manage user profiles (People), and integrate with Mixpanel's feature flagging system. It is currently at version 5.1.0 and maintains an active release cadence, frequently adding new features and ensuring compatibility with modern Python versions.","status":"active","version":"5.1.0","language":"en","source_language":"en","source_url":"https://github.com/mixpanel/mixpanel-python","tags":["analytics","mixpanel","events","tracking","user-behavior","feature-flags"],"install":[{"cmd":"pip install mixpanel","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Used for HTTP communication with the Mixpanel API, introduced in v4.9.0 to improve TLS certificate handling.","package":"requests","optional":false}],"imports":[{"note":"The primary client class is directly importable from the top-level package.","wrong":"import mixpanel; mixpanel.Mixpanel(...)","symbol":"Mixpanel","correct":"from mixpanel import Mixpanel"}],"quickstart":{"code":"import os\nfrom mixpanel import Mixpanel\n\n# Replace with your Mixpanel Project Token from environment variable or direct string\nMIXPANEL_TOKEN = os.environ.get('MIXPANEL_TOKEN', 'YOUR_MIXPANEL_PROJECT_TOKEN')\n\nif not MIXPANEL_TOKEN or MIXPANEL_TOKEN == 'YOUR_MIXPANEL_PROJECT_TOKEN':\n    print(\"Warning: MIXPANEL_TOKEN not set. Using placeholder. Events will not be sent to Mixpanel.\")\n\n# Initialize the Mixpanel client\n# For EU data residency, use api_region='EU'\nmp = Mixpanel(MIXPANEL_TOKEN, api_region='US')\n\n# Track an event\nuser_id = 'user123'\nevent_name = 'Signup Success'\nproperties = {'source': 'website', 'plan': 'premium'}\nmp.track(user_id, event_name, properties)\nprint(f\"Tracked event '{event_name}' for user '{user_id}'\")\n\n# Update user profile (People properties)\npeople_properties = {'$first_name': 'John', '$last_name': 'Doe', 'plan': 'premium'}\nmp.people_set(user_id, people_properties)\nprint(f\"Set people properties for user '{user_id}'\")\n\n# Increment a people property\nmp.people_increment(user_id, {'Login Count': 1})\nprint(f\"Incremented 'Login Count' for user '{user_id}'\")\n\n# For non-web applications, ensure events are flushed before exiting\nmp.flush()\nprint(\"Mixpanel client flushed.\")","lang":"python","description":"This quickstart demonstrates how to initialize the Mixpanel client, track an event, and update user profiles. Ensure your Mixpanel Project Token is set via an environment variable or directly in the code. For applications that terminate quickly (e.g., scripts, serverless functions), explicitly calling `mp.flush()` is crucial to ensure all queued events are sent before the process exits."},"warnings":[{"fix":"Replace `Mixpanel(token, api_host='https://api.mixpanel.com')` with `Mixpanel(token, api_region='US')` or `api_region='EU'` as appropriate.","message":"The `api_host` parameter for initializing the `Mixpanel` client was removed in v5.0.0. Use the `api_region` parameter (`'US'` or `'EU'`) instead to specify the data residency region.","severity":"breaking","affected_versions":">=5.0.0"},{"fix":"Upgrade your Python environment to 3.9 or higher, or pin your `mixpanel` dependency to `<4.11.0` (e.g., `pip install 'mixpanel<4.11.0'`).","message":"Support for Python versions older than 3.9 was dropped in version 4.11.0. If you are on an older Python version, you must use a `mixpanel` library version less than 4.11.0.","severity":"breaking","affected_versions":">=4.11.0"},{"fix":"For critical events, call `mp.flush()` explicitly. For high-throughput scenarios, integrate with an asynchronous task queue (e.g., Celery) or a custom event buffer that flushes periodically.","message":"The `Mixpanel` client operates synchronously by default. In long-running applications or those handling high event volumes, consider using asynchronous processing or a separate queueing mechanism to avoid blocking your main application thread. Always call `mp.flush()` to ensure all buffered events are sent before your application exits.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Upgrade to `mixpanel` version 4.10.1 or higher to benefit from the connection stability fixes.","message":"Versions prior to 4.10.1 were prone to 'connection reset by peer' errors, especially during long-running sessions or network instability. This issue was resolved in v4.10.1.","severity":"gotcha","affected_versions":"<4.10.1"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}