> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/mixpanel/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Python SDK

> Track events from Python servers with the Mixpanel server-side SDK

## Installation

```bash theme={null}
pip install mixpanel
```

## Initialize

```python theme={null}
from mixpanel import Mixpanel

mp = Mixpanel("YOUR_PROJECT_TOKEN")
```

### Configuration

```python theme={null}
import mixpanel

mp = mixpanel.Mixpanel(
    "YOUR_PROJECT_TOKEN",
    consumer=mixpanel.Consumer(retry_limit=2)
)
```

## Track Events

<Warning>
  You must provide a `distinct_id` for all events.
</Warning>

```python theme={null}
mp.track('12345', 'Purchase', {
    'product': 'Premium Subscription',
    'amount': 49.99,
    'currency': 'USD',
    'ip': '192.168.0.1'
})
```

## Import Historical Events

For events older than 5 days:

```python theme={null}
mp.import_data(
    api_key='',  # Deprecated but required
    distinct_id='12345',
    event_name='Old Purchase',
    timestamp=1634528073,
    properties={'amount': 100},
    api_secret='YOUR_API_SECRET'
)
```

## User Profiles

```python theme={null}
mp.people_set('12345', {
    'name': 'John Doe',
    '$email': 'john@example.com',
    'plan': 'Premium'
}, meta={
    '$ignore_time': True,  # Don't update $last_seen
    '$ip': 0  # Disable geolocation
})
```

<Tabs>
  <Tab title="people_set_once()">
    ```python theme={null}
    mp.people_set_once('12345', {
        'First Purchase': '2024-01-01'
    }, meta={'$ip': 0})
    ```
  </Tab>

  <Tab title="people_increment()">
    ```python theme={null}
    mp.people_increment('12345', {'login_count': 1})
    mp.people_increment('12345', {'age': 1})
    ```
  </Tab>

  <Tab title="people_append()">
    ```python theme={null}
    mp.people_append('12345', {
        'transactions': 'purchase_123'
    }, meta={'$ip': 0})
    ```
  </Tab>

  <Tab title="people_union()">
    ```python theme={null}
    mp.people_union('12345', {
        'skills': ['Python', 'Django']
    }, meta={'$ip': 0})
    ```
  </Tab>
</Tabs>

## Group Analytics

### Send Events

```python theme={null}
mp.track('12345', 'Feature Used', {
    'company': 'Acme Inc',
    'feature': 'Reports'
})
```

### Set Group Properties

```python theme={null}
mp.group_set('company', 'Acme Inc', {
    'name': 'Acme Inc',
    'industry': 'Technology',
    'employees': 500
})
```

<Tabs>
  <Tab title="group_set_once()">
    ```python theme={null}
    mp.group_set_once('company', 'Acme Inc', {
        'founded': '2010-01-01'
    })
    ```
  </Tab>

  <Tab title="group_unset()">
    ```python theme={null}
    mp.group_unset('company', 'Acme Inc', [
        'temp_property'
    ])
    ```
  </Tab>

  <Tab title="group_union()">
    ```python theme={null}
    mp.group_union('company', 'Acme Inc', {
        'features': ['Reports', 'Analytics']
    })
    ```
  </Tab>

  <Tab title="group_remove()">
    ```python theme={null}
    mp.group_remove('company', 'Acme Inc', {
        'features': ['Beta Feature']
    })
    ```
  </Tab>
</Tabs>

## Privacy Controls

### EU Data Residency

```python theme={null}
from mixpanel import Mixpanel
import mixpanel

mp = Mixpanel(
    "YOUR_PROJECT_TOKEN",
    consumer=mixpanel.Consumer(api_host="api-eu.mixpanel.com")
)
```

### India Data Residency

```python theme={null}
mp = Mixpanel(
    "YOUR_PROJECT_TOKEN",
    consumer=mixpanel.Consumer(api_host="api-in.mixpanel.com")
)
```

### Geolocation

<Info>
  The Python SDK sets IP to `0` by default, meaning geolocation is not tracked unless explicitly included.
</Info>

```python theme={null}
# Explicitly set IP for geolocation
mp.track('12345', 'event', {
    'ip': '192.168.0.1'
})
```

## Platform Considerations

<Warning>
  All server-side calls originate from your server's IP. Set `$ip` to `0` to disable geolocation or provide user's actual IP.
</Warning>

* You manage `distinct_id` yourself
* No automatic identity management
* IP defaults to `0` (no geolocation)
* Use `.import_data()` for historical events
* Designed for scripting and batch operations

## Resources

* [Full API Reference](http://mixpanel.github.io/mixpanel-python)
* [GitHub Repository](https://github.com/mixpanel/mixpanel-python)
* [Example Script](https://github.com/mixpanel/mixpanel-python/tree/master/demo)
