Python is great at failing silently. A background task crashes, a scheduled job never logs, an unhandled exception in a thread dies without a trace. The traceback goes nowhere, your user's workflow breaks, and you have no idea it ever happened. Python error tracking is how you surface those invisible crashes — capturing every uncaught exception automatically, grouping them by root cause, and alerting you before your users file a support ticket.
This guide shows how to instrument any Python app with the Sentry SDK and LightTrace, so exceptions land in a fast, mobile-first dashboard instead of swallowed by your runtime.
Why Python needs error tracking
Python applications run in a lot of environments: web servers with dozens of worker threads, async frameworks handling thousands of concurrent requests, background job processors churning through queues at 2 a.m. Most of those environments don't have a built-in way to tell you when something goes wrong—logs are there if you look for them, but you can't monitor every log file in real time.
Worse, some failures never hit a log at all. An unhandled exception in a thread ends silently. A callback queued to an event loop and then deleted never runs—no error, no trace. Your payment API times out and retries tomorrow, and the transaction sits in limbo for 18 hours before someone notices it in a report.
Error tracking solves this by installing global handlers at startup so every crash—whether in your main thread, a background worker, or an async callback—gets captured and forwarded. You stop discovering bugs in production through support complaints.
Install and initialize the SDK
Add the Sentry SDK for Python (it works across Django, Flask, FastAPI, and plain Python):
pip install sentry-sdk
Initialize it early in your app startup, before any of your application code runs:
import sentry_sdk
sentry_sdk.init(
dsn="https://<key>@your-lighttrace-host/1",
environment="production",
release="api@2.1.5",
traces_sample_rate=1.0,
)
# From here on, all unhandled exceptions are captured.
That's it. From that point forward, any uncaught exception—a KeyError in a request handler, a TypeError in a scheduled job, an OSError from a network call that fails—gets caught and sent to LightTrace. Because LightTrace is Sentry-SDK-compatible, you're using the standard SDK and pointing it at LightTrace by changing only the dsn.
Framework-specific integration
The real power comes from framework integrations. Sentry auto-detects your framework and wires error capturing into the right places—middleware for Django and Flask, middleware for async ASGI frameworks like FastAPI, exception handlers for background jobs.
Django
Django catches most request exceptions automatically, but the SDK hooks into Django's signal system and the error page renderer to capture them before they turn into 500s:
# settings.py
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration
sentry_sdk.init(
dsn="https://<key>@your-lighttrace-host/1",
integrations=[DjangoIntegration()],
)
Now every unhandled exception in a view, a middleware, or a signal handler gets captured with the full request context: the URL, the user, HTTP headers, and query parameters.
Flask
Flask doesn't have a global error handler by default—exceptions in route handlers and before/after request handlers need to be caught explicitly. The SDK integration taps into Flask's error-handling hooks:
# app.py
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
sentry_sdk.init(
dsn="https://<key>@your-lighttrace-host/1",
integrations=[FlaskIntegration()],
)
app = Flask(__name__)
After this, Flask automatically captures exceptions and attaches request context to every event.
FastAPI
FastAPI is built on Starlette and speaks ASGI, so async exceptions can be tricky to catch—but the Sentry integration wraps the app's exception handling middleware:
# main.py
import sentry_sdk
from sentry_sdk.integrations.fastapi import FastApiIntegration
from fastapi import FastAPI
sentry_sdk.init(
dsn="https://<key>@your-lighttrace-host/1",
integrations=[FastApiIntegration()],
)
app = FastAPI()
Every unhandled exception in an async route, a dependency, or middleware gets captured with breadcrumbs showing the async call chain.
Add context so errors are fast to fix
A stack trace is a starting point, but context is what makes fixing fast. Tell LightTrace who the user is, which release introduced the crash, and what they were doing when it broke:
import sentry_sdk
# Set the user so you know how many users hit the crash
sentry_sdk.set_user({"id": user.id, "email": user.email})
# Tag each error with release so you can spot the deploy that broke it
sentry_sdk.set_tag("release", "api@2.1.5")
# Add breadcrumbs to show the path that led to the crash
sentry_sdk.capture_breadcrumb(
category="checkout",
message="Attempting charge",
level="info",
)
# If you catch an exception and want to report it manually:
try:
result = stripe.Charge.create(...)
except stripe.error.CardError as e:
sentry_sdk.capture_exception(e)
Breadcrumbs are critical for async Python apps. They show the sequence of async task switches, queue submissions, and external calls that led to a crash. LightTrace captures breadcrumbs automatically from log records and database queries, but add your own at business-logic checkpoints.
Now every error arrives with the user, the release, and the full trail of events leading up to it. Instead of "there was a crash," you have "user abc@example.com, who placed an order last week, hit this error after clicking checkout" — enough to reproduce it locally or hit it with a quick test.
Grouping turns noise into signal
If your app handles 10,000 requests per second, a single unhandled exception could throw hundreds of events before you even fix it. Without grouping, that's hundreds of alerts. With grouping, it's one issue with a count.
LightTrace groups events by fingerprint — a hash of the stack trace's top frames. Two events with the same fingerprint are considered the same bug and collapsed into a single issue, even if they occurred hours apart or across different machines.
Sometimes the automatic fingerprint is wrong — for instance, if your exception message includes a timestamp or user ID, every error looks unique. You can override the fingerprint to group by what actually matters: the line of code that broke, not the runtime data around it.
With grouping in place, your dashboard shows a short, ranked list of real bugs. The crash hitting 40,000 users sits at the top. The background task that fails once a week sits below it. One glance tells you what to fix first.
Common Python exceptions to watch for
Some exceptions show up so often in Python that they deserve their own section:
- NoneType object is not iterable — you tried to loop over a function that returned
None. AttributeError: 'NoneType' object has no attribute 'x'— same root cause, different path through your code.KeyErrorin a dictionary lookup — unguarded access to a key that might not exist.ValueErrorin integer or datetime parsing — the input was malformed.ConnectionErrorfrom a network call — timeouts, DNS failures, connection refused.
Error tracking doesn't fix these, but it tells you which ones are actually hitting users and how often. That's when you prioritize the fix.
Debug production with unreadable backtraces
Stack traces from production Python are usually readable—you have source code on disk—but deployed packages sometimes obscure the context. If you're running bytecode-only packages or deployed code differs from your repository, LightTrace pairs the stack trace with breadcrumbs and context to help you reconstruct what went wrong.
For a deeper dive into the workflow, see how to debug production errors.
Release tagging and deploy tracking
Tag every deployment with a release identifier so you can tie crashes to the deploy that introduced them:
sentry_sdk.init(
dsn="https://<key>@your-lighttrace-host/1",
release="api@2.1.5",
environment="production",
)
Now when a new error appears right after a deploy, LightTrace shows that it's associated with the release, and you can quickly roll back or decide whether the error rate is acceptable. See release health monitoring for more.
Start capturing today
The payoff is simple: fewer surprises, faster fixes, and a dashboard that tells you which bugs matter. Add error tracking to any Python app takes ten minutes and works identically across Django, Flask, FastAPI, or raw WSGI.
Start tracking errors in minutes
Point the Sentry SDK at LightTrace and see your first grouped Python exceptions in minutes — free up to 5,000 events a month.
For framework-specific setup, see the full guides for Django, Flask, or FastAPI. Otherwise, the pattern above works for any Python application. Deploy, monitor, and commit to fixing the errors that matter.