# Python Error Monitoring & Performance Monitoring

> Diagnose failures in Django, Flask, FastAPI, and Celery with local variables in the stack, function-level profiling, and logs. Free to start.

**URL:** https://sentry.io/for/python/

---

Python error monitoring with actionable insight into every exception. See the full picture behind any Python error, resolve performance bottlenecks, and get framework-specific coverage that spans Django application monitoring, Flask, FastAPI, and Celery.

**Documentation:** [https://docs.sentry.io/platforms/python/](https://docs.sentry.io/platforms/python/)

## Install

### Python

Grab the [Sentry Python SDK](https://github.com/getsentry/sentry-python):

```bash
pip install --upgrade sentry-sdk
```

Initialize the SDK as early as possible in your application:

```python
import sentry_sdk

sentry_sdk.init(
    dsn="https://<key>@sentry.io/<project>",
    # Add data like request headers and IP for users, if applicable;
    # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
    send_default_pii=True,
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    # We recommend adjusting this value in production.
    traces_sample_rate=1.0,
)
```

See the [Python docs](https://docs.sentry.io/platforms/python/) for full setup options.

### Django

Install our Python SDK using [pip](https://pip.pypa.io/en/stable/):

```bash
pip install --upgrade sentry-sdk
```

Initialize the SDK in your `settings.py` file. The Django integration is enabled automatically.

```python
import sentry_sdk

sentry_sdk.init(
    dsn="https://<key>@sentry.io/<project>",
    # Add data like request headers and IP for users, if applicable;
    # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
    send_default_pii=True,
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    # We recommend adjusting this value in production.
    traces_sample_rate=1.0,
)
```

See the [Django docs](https://docs.sentry.io/platforms/python/integrations/django/) for full setup options.

### Flask

Install the [Sentry Python SDK](https://github.com/getsentry/sentry-python) from PyPI:

```bash
pip install --upgrade sentry-sdk
```

Initialize the SDK before creating your app. The Flask integration is enabled automatically.

```python
import sentry_sdk
from flask import Flask

sentry_sdk.init(
    dsn="https://<key>@sentry.io/<project>",
    # Add data like request headers and IP for users, if applicable;
    # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
    send_default_pii=True,
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    # We recommend adjusting this value in production.
    traces_sample_rate=1.0,
)

app = Flask(__name__)
```

See the [Flask docs](https://docs.sentry.io/platforms/python/integrations/flask/) for full setup options.

### FastAPI

Grab the [Sentry Python SDK](https://github.com/getsentry/sentry-python):

```bash
pip install --upgrade sentry-sdk
```

Initialize the SDK before creating your app. The FastAPI integration is enabled automatically.

```python
import sentry_sdk
from fastapi import FastAPI

sentry_sdk.init(
    dsn="https://<key>@sentry.io/<project>",
    # Add data like request headers and IP for users, if applicable;
    # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
    send_default_pii=True,
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    # We recommend adjusting this value in production.
    traces_sample_rate=1.0,
)

app = FastAPI()
```

See the [FastAPI docs](https://docs.sentry.io/platforms/python/integrations/fastapi/) for full setup options.

### Bottle

Install the [Sentry Python SDK](https://github.com/getsentry/sentry-python) from PyPI:

```bash
pip install --upgrade sentry-sdk
```

Initialize the SDK before your app is created. The Bottle integration is enabled automatically.

```python
import sentry_sdk
from bottle import Bottle

sentry_sdk.init(
    dsn="https://<key>@sentry.io/<project>",
    # Add data like request headers and IP for users, if applicable;
    # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
    send_default_pii=True,
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    # We recommend adjusting this value in production.
    traces_sample_rate=1.0,
)

app = Bottle()
```

See the [Bottle docs](https://docs.sentry.io/platforms/python/integrations/bottle/) for full setup options.

### Celery

Install the [Sentry Python SDK](https://github.com/getsentry/sentry-python) from PyPI:

```bash
pip install --upgrade sentry-sdk
```

Initialize the SDK when the Celery worker starts (not only in the module where your tasks are defined). The Celery integration is enabled automatically.

```python
import sentry_sdk
from celery import Celery, signals

app = Celery("tasks", broker="...")

@signals.celeryd_init.connect
def init_sentry(**_kwargs):
    sentry_sdk.init(
        dsn="https://<key>@sentry.io/<project>",
        # Add data like request headers and IP for users, if applicable;
        # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
        send_default_pii=True,
        # Set traces_sample_rate to 1.0 to capture 100%
        # of transactions for tracing.
        # We recommend adjusting this value in production.
        traces_sample_rate=1.0,
    )
```

If you use Celery with Django and already initialize Sentry in `settings.py`, no extra setup is needed.

See the [Celery docs](https://docs.sentry.io/platforms/python/integrations/celery/) for full setup options.

### Pyramid

Install the [Sentry Python SDK](https://github.com/getsentry/sentry-python) from PyPI:

```bash
pip install --upgrade sentry-sdk
```

Initialize the SDK before or after your app has been created. The Pyramid integration is enabled automatically.

```python
import sentry_sdk
from pyramid.config import Configurator

sentry_sdk.init(
    dsn="https://<key>@sentry.io/<project>",
    # Add data like request headers and IP for users, if applicable;
    # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
    send_default_pii=True,
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    # We recommend adjusting this value in production.
    traces_sample_rate=1.0,
)

with Configurator() as config:
    # ...
```

See the [Pyramid docs](https://docs.sentry.io/platforms/python/integrations/pyramid/) for full setup options.

### Tornado

Install the [Sentry Python SDK](https://github.com/getsentry/sentry-python) from PyPI:

```bash
pip install --upgrade sentry-sdk
```

Initialize the SDK before your app starts. The Tornado integration is enabled automatically.

```python
import sentry_sdk

sentry_sdk.init(
    dsn="https://<key>@sentry.io/<project>",
    # Add data like request headers and IP for users, if applicable;
    # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
    send_default_pii=True,
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    # We recommend adjusting this value in production.
    traces_sample_rate=1.0,
)

# Your app code here, without changes

class MyHandler(...):
    # ...
```

See the [Tornado docs](https://docs.sentry.io/platforms/python/integrations/tornado/) for full setup options.

### RQ

Install the [Sentry Python SDK](https://github.com/getsentry/sentry-python) from PyPI:

```bash
pip install --upgrade sentry-sdk
```

Create a file called `mysettings.py` with the following content. The RQ integration is enabled automatically.

```python
import sentry_sdk

sentry_sdk.init(
    dsn="https://<key>@sentry.io/<project>",
    # Add data like request headers and IP for users, if applicable;
    # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
    send_default_pii=True,
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    # We recommend adjusting this value in production.
    traces_sample_rate=1.0,
)
```

Start your worker with:

```bash
rq worker -c mysettings
```

See the [RQ docs](https://docs.sentry.io/platforms/python/integrations/rq/) for full setup options.

### AWS Lambda Python

Install our Python SDK into your deployment package using `pip`:

```bash
pip install --target ./package --upgrade sentry-sdk
```

Then use the AWS Lambda integration for the Python SDK like this:

```python
import sentry_sdk
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration

sentry_sdk.init(
    dsn="https://<key>@sentry.io/<project>",
    # Add data like request headers and IP for users, if applicable;
    # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
    send_default_pii=True,
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    # We recommend adjusting this value in production.
    traces_sample_rate=1.0,
    integrations=[AwsLambdaIntegration()],
)

def lambda_handler(event, context):
    # ...
```

See the [AWS Lambda Python docs](https://docs.sentry.io/platforms/python/integrations/aws-lambda/) for full setup options.

### Google Cloud Functions Python

Add the Sentry SDK to your `requirements.txt`:

```text
sentry-sdk
```

Then use the GCP Functions integration for the Python SDK like this:

```python
import sentry_sdk
from sentry_sdk.integrations.gcp import GcpIntegration

sentry_sdk.init(
    dsn="https://<key>@sentry.io/<project>",
    # Add data like request headers and IP for users, if applicable;
    # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
    send_default_pii=True,
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    # We recommend adjusting this value in production.
    traces_sample_rate=1.0,
    integrations=[GcpIntegration()],
)

def http_function_entrypoint(request):
    # ...
```

The GCP integration supports the Python 3.7 runtime. On newer runtimes, use the [`serverless_function` decorator](https://docs.sentry.io/platforms/python/integrations/serverless/) instead.

See the [Google Cloud Functions Python docs](https://docs.sentry.io/platforms/python/integrations/gcp-functions/) for full setup options.

## Features

### Python Performance Monitoring

Within minutes after installing Sentry, software teams can trace Python [performance](/product/tracing/) issues back to a poor-performing API call and surface every related code error. From Django application monitoring to FastAPI performance, engineering managers and developers get a single tool to optimize their code and deliver fast customer experiences.

### Complete Stack Traces, With Local Variables

See local variables in the stack for prod errors, just like in your dev environment. Introspect more deeply into the runtime and jump into the frame to get additional data for any local variable. Filter and group Python exceptions intuitively to eliminate noise, whether you're doing Flask error handling, tracking Django views, or monitoring Celery jobs.

### Fill In the Blanks

Expose the important events that led to each Python exception: SQL queries, debug logs, network requests, past errors. Improve debugging workflow with a full view of releases so you can mark errors as resolved and prioritize live issues.

### Profiling, Down to the Function

Profiling lets you see what parts of your code are consuming the most resources, like CPU or memory, in your application— so you can optimize them before end user experience is impacted. Test your application performance in any environment, including in production, without writing manual tests or extensive troubleshooting.

### Your Logs, Next to the Trace

Ship your Python logging output to Sentry and query it next to the [trace](/product/tracing/) that produced it. The `logging` calls you already write become searchable context on the exact request that failed, so retries, third-party API failures, and internal state are visible without a second tool.

Learn more about [Logs in Sentry](/product/logs/).

## FAQ

**What is the difference between Sentry and traditional logging?**

Traditional logging provides you with a trail of events. Some of those events are errors, but many times they're simply informational. Sentry is fundamentally different because we focus on exceptions, or in other words, we capture application crashes. We discuss in more detail [here](/vs/logging/) and on our [blog](https://blog.sentry.io/lets-get-ready-to-monitor/).

**What  languages does Sentry support?**

Sentry supports every major  language, framework, and library. You can browse each of them [here](/platforms/).

**How much does Sentry cost?**

You can get started for free. Pricing depends on the number of monthly events, transactions, and attachments that you send Sentry. For more details, visit our [pricing page](/pricing/).

**How does Sentry impact the performance of my app?**

Sentry doesn't impact a web site's performance.

If you look at the configuration options for when you initialize Sentry in your code, you'll see there's nothing regarding minimizing its impact on your app's performance. This is because our team of SDK engineers already developed Sentry with this in mind.

Sentry is a listener/handler for errors that asynchronously sends out the error/event to Sentry.io. This is non-blocking. The error/event only goes out if this is an error.

Global handlers have almost no impact as well, as they are native APIs provided by the browsers.

**Can I send logs to Sentry?**

Yes. Sentry Logs lets you send, view, and query structured logs from your application alongside your errors and traces. Because logs sit next to the error and the trace that produced them, you get the granular context — retries, API failures, internal state — that spans and stack traces alone can miss, without switching tools.

Learn more about [Logs in Sentry](/product/logs/).

**What is Seer, Sentry's AI debugging agent?**

Seer is Sentry's AI debugging agent. It reads the full context Sentry already has — the error, its stack trace, the surrounding trace, logs, and profiles — to find the root cause of an issue and propose a fix. Seer can open a pull request with that fix, hand the work to an external coding agent, or review your PRs before they ship.

Learn more about [Seer](/product/seer/) and [Autofix](/product/seer/autofix/).

---

*Source: [sentry.io/for/python/](https://sentry.io/for/python/)*
