---
title: "Basic Usage"
url: https://docs.sentry.io/platforms/python/legacy-sdk/usage/
---

# Basic Usage | Sentry for Python

##### Deprecation Warning

A new Python SDK has superseded this deprecated version. Sentry preserves this documentation for customers using the old client. We recommend using the [updated Python SDK](https://docs.sentry.io/platforms/python.md) for new projects.

This gives a basic overview of how to use the raven client with Python directly.

## [Capture an Error](https://docs.sentry.io/platforms/python/legacy-sdk/usage.md#capture-an-error)

The most basic use for raven is to record one specific error that occurs:

```python
from raven import Client

client = Client('___DSN___')

try:
    1 / 0
except ZeroDivisionError:
    client.captureException()
```

## [Reporting an Event](https://docs.sentry.io/platforms/python/legacy-sdk/usage.md#reporting-an-event)

To report an arbitrary event you can use the [`capture()`](https://docs.sentry.io/platforms/python/legacy-sdk/api.md#raven.Client.capture "raven.Client.capture") method. This is the most low-level method available. In most cases you would want to use the [`captureMessage()`](https://docs.sentry.io/platforms/python/legacy-sdk/api.md#raven.Client.captureMessage "raven.Client.captureMessage") method instead however which directly reports a message:

```python
client.captureMessage('Something went fundamentally wrong')
```

## [Adding Context](https://docs.sentry.io/platforms/python/legacy-sdk/usage.md#adding-context)

The raven client internally keeps a thread local mapping that can carry additional information. Whenever a message is submitted to Sentry that additional data will be passed along.

For instance if you use a web framework, you can use this to inject additional information into the context. The basic primitive for this is the [`context`](https://docs.sentry.io/platforms/python/legacy-sdk/api.md#raven.Client.context "raven.Client.context") attribute. It provides a *merge()* and *clear()* function that can be used:

```python
def handle_request(request):
    client.context.merge({'user': {
        'email': request.user.email
    }})
    try:
        ...
    finally:
        client.context.clear()
```

Additionally starting with Raven 5.14 you can bind the context to the current thread to enable crumb support by calling *activate()*. The deactivation happens upon calling *clear()*. This can also be achieved by using the context object with the *with* statement. This is needed to enable breadcrumb capturing. Framework integrations typically do this automatically.

These two examples are equivalent:

```python
def handle_request(request):
    client.context.activate()
    client.context.merge({'user': {
        'email': request.user.email
    }})
    try:
        ...
    finally:
        client.context.clear()
```

With a context manager:

```python
def handle_request(request):
    with client.context:
        client.context.merge({'user': {
            'email': request.user.email
        }})
        try:
            ...
        finally:
            client.context.clear()
```

## [Testing the Client](https://docs.sentry.io/platforms/python/legacy-sdk/usage.md#testing-the-client)

Once you’ve got your server configured, you can test the Raven client by using its CLI:

```bash
raven test ___DSN___
```

If you’ve configured your environment to have `SENTRY_DSN` available, you can simply drop the optional DSN argument:

```bash
raven test
```

You should get something like the following, assuming you’re configured everything correctly:

```bash
raven test sync+___DSN___
Using DSN configuration:
  sync+___DSN___

Client configuration:
  servers        : ___API_URL___/api/store/
  project        : ___PROJECT_ID___
  public_key     : ___PUBLIC_KEY___
  secret_key     : ___SECRET_KEY___

Sending a test message... success!
```
