Python

For pairing Sentry up with Python you can use the Raven for Python (raven-python) library. It is the official standalone Python client for Sentry. It can be used with any modern Python interpreter be it CPython 2.x or 3.x, PyPy or Jython. It’s an Open Source project and available under a very liberal BSD license.

Installation

If you haven’t already, start by downloading Raven. The easiest way is with pip:

Copied
pip install raven --upgrade

Configuring the Client

Settings are specified as part of the initialization of the client. The client is a class that can be instantiated with a specific configuration and all reporting can then happen from the instance of that object. Typically an instance is created somewhere globally and then imported as necessary. For getting started all you need is your DSN:

Copied
from raven import Client
client = Client('https://examplePublicKey@o0.ingest.sentry.io/0')

Capture an Error

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

Copied
from raven import Client

client = Client('https://examplePublicKey@o0.ingest.sentry.io/0')

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

Adding Context

Much of the usefulness of Sentry comes from additional context data with the events. The Python client makes this very convenient by providing methods to set thread local context data that is then submitted automatically with all events. For instance you can use user_context() to set the information about the current user:

Copied
def handle_request(request):
    client.user_context({
        'email': request.user.email
    })

Deep Dive

Raven Python is more than that however. To dive deeper into what it does, how it works and how it integrates into other systems there is more to discover:

Resources: