Installing Airbrake in a Python app

python flag

Features

  • Simple to install and configure
  • Integrates with your existing logger
  • Add extra context to errors before they are sent
  • Set error severity and control notification thresholds
  • Compatible with Airbrake On-Premise

What version of Python are you using?

The version of Python your app is using determines which installation instrutcions to follow.

Python 3.x+

Installation

The pybrake notifier supports Python 3.4 and above.

pip install -U pybrake

Configuration

To configure pybrake you will need your Airbrake project’s id and api_key, these are available from your project’s settings page.

import pybrake

notifier = pybrake.Notifier(project_id=123,
                            project_key='FIXME',
                            environment='production')

Sending errors to Airbrake

try:
    raise ValueError('hello')
except Exception as err:
    notifier.notify(err)

Taking pybrake further

Now that pybrake is configured and ready to go we recommend you check out some of the additional integrations and features pybrake supports:

Visit the pybrake GitHub repo for full details.

Python 2.7

Not on Python 3.4+ yet? No problem! Follow these instructions and you will be capturing errors in no time.

Installation

pip install -U airbrake

Configuration

Automatic configuration

The easiest way to get set up is with a few environment variables. You can find your PROJECT ID and PROJECT API KEY on your project’s settings page.

export AIRBRAKE_API_KEY='<Your project API KEY>'
export AIRBRAKE_PROJECT_ID='<Your project ID>'
export AIRBRAKE_ENVIRONMENT=production

and you’re done! These environment variables are used to automatically configure your AirbrakeHandler.

Manual configuration

Or, you can manually instantiate your AirbrakeHandler by passing your PROJECT ID and PROJECT API KEY as arguments to the getLogger() helper.

import airbrake

logger = airbrake.getLogger(api_key="<Your project API KEY>", project_id=<Your project ID>)

try:
    1/0
except Exception:
    logger.exception("Bad math.")

Taking airbrake-python further

Now that you have configured Airbrake to report exceptions from your Python 2.7 app, we recommend you add extra context to your errors and add Airbrake to your existing logger. Please visit the airbrake-python GitHub repo for the full list of notifier features.