Python

On this page, we provide concise information to get you up and running with Sentry's Python SDK, automatically reporting errors and exceptions in your application.

Using a framework? Take a look at our specific guides to get started.

Install

Sentry captures data by using an SDK within your application’s runtime. These are platform specific and allow Sentry to have a deep understanding of how your application works.

Install our Python SDK using pip:

Copied
$ pip install --upgrade sentry-sdk

The SDK provides support for Python 2.7 and 3.4 or later. Integrations with specific frameworks (particularly asynchronous ones) may impose additional requirements though.

Configure

After you’ve completed setting up a project in Sentry, Sentry will give you a value which we call a DSN or Data Source Name. It looks a lot like a standard URL, but it’s just a representation of the configuration required by the Sentry SDKs. It consists of a few pieces, including the protocol, public key, the server address, and the project identifier.

Import and initialize the Sentry SDK early in your application’s setup:

Copied
import sentry_sdk
sentry_sdk.init(dsn='https://examplePublicKey@o0.ingest.sentry.io/0')

Verify

Great! Now that you’ve completed setting up the SDK, maybe you want to quickly test out how Sentry works. Start by capturing an exception:

Copied
sentry_sdk.capture_exception(Exception("This is an example of an error message."))

Once the exception is captured, you'll see the error in your Sentry dashboard.

Usage

In Python you can either capture a caught exception or the one currently held in sys.exc_info() by not passing an argument:

Copied
from sentry_sdk import capture_exception

try:
    a_potentially_failing_function()
except Exception as e:
    # Alternatively the argument can be omitted
    capture_exception(e)

Monitor Performance

To get started with performance monitoring using Sentry's Python SDK, first install sentry_sdk:

Copied
pip install --upgrade sentry-sdk

Next, initialize the integration in your call to sentry_sdk.init:

Copied
import sentry_sdk

sentry_sdk.init(
    "https://examplePublicKey@o0.ingest.sentry.io/0",
    traces_sample_rate=1.0 # be sure to lower this in production to prevent quota issues
)

Performance data is transmitted using a new event type called "transactions", which you can learn about in Distributed Tracing. To capture transactions, you must install and configure your SDK to set the traces_sample_rate option to a nonzero value. The example configuration above will transmit 100% of captured traces. Be sure to lower this value in production otherwise you could burn through your quota quickly.

Learn more about sampling in Using Your SDK to Filter Events.

You can edit this page on GitHub.