Honeybadger + Python

Attention Pythonistas: Honeybadger now supports reporting exceptions from Python and Django applications!

Attention Pythonistas: Honeybadger now supports reporting exceptions from Python and Django applications!

It's true that IRL honey badgers may not get along with IRL pythons, but on the internets they are now the best of friends thanks to our new honeybadger-python client package. In this post we'll discuss some of the features we have already built, how to set everything up, and what we plan to add in the future.


To get started, install our package with pip:

$ pip install honeybadger

For Django

In a Django application, add the Honeybadger Django middleware to the top of your MIDDLEWARE_CLASSES config variable:


You'll also need to add a new HONEYBADGER config variable to your settings.py to specify your API key:

  'API_KEY': 'myapikey'

That's it! We'll report all unhandled exceptions which happen during your Django web requests. We also automatically include a lot of useful request data, such as parameters and session information:

For other frameworks/plain Python apps

Django is the only explicitly supported framework at the moment. For other frameworks (Flask, web2py, etc.) or a plain Python script, simply import honeybadger and configure it with your API key. Honeybadger uses a global exception hook to automatically report any uncaught exceptions.

from honeybadger import honeybadger

raise Exception, "This will get reported!"

Reporting exceptions manually

In some cases you may want to catch an exception using a try statement, in which case it won't be caught by the global exception handler. Instead, you can use the Honeybadger.notify method:

mydict = dict(a=1)
  print mydict['b']
except KeyError, exc:
  honeybadger.notify(exc, context={'foo': 'bar'})

You can also use Honeybadger.notify to report an arbitrary error, without an exception object:

honeybadger.notify(error_class='ValueError', error_message='Something bad happened!')

We'll even automatically generate a traceback from the location the method is called.

Adding context

Now that Honeybadger is recording your exceptions it's time to think about context. Use Honeybadger.set_context to add extra contextual data which will be reported with any errors which occur in the current transaction. For example, let's say we want to track which user is currently logged in:


This data will be displayed in Honeybadger with every error that gets reported:

You can use honeybadger.reset_context to reset any context which has already been added and start fresh. In a Django web request we do this for you automatically.

If you don't want to set global context data, you can use our Python context manager to set case-specific contextual information:

# from a Django view
from honeybadger import honeybadger
def my_view(request):
  with honeybadger.context(user_email=request.POST.get('user_email', None)):
    form = UserForm(request.POST)
    # ...

Filtering sensitive data

In some cases you may not want to send certain types of sensitive request data to Honeybadger, such as passwords and credit cards. You can configure keys to exclude from the data that we collect using the params_filters config option:

  params_filters=['super', 'secret', 'keys']

By default we filter out "password", "password_confirmation", and "credit_card" (you will need to include these in the list if you provide your own params_filters, though):

What's next?

This is the first release of Honeybadger for Python, so there's plenty of work to do!

Next up we want to add the ability to report error level logs from Python's logging framework, add support for more frameworks such as Flask, support sending request metrics/slow request traces, and more.

We'd also like to support more versions of Python and Django (the package is currently tested against Django 1.9 and Python 2.7).

Feedback is welcome! Have a comment, question, suggestion, or bug report? Email me, submit an issue, or send a pull request.

Need a Honeybadger account? Start your free trial and exterminate your Python errors today!

Honeybadger has your back when it counts. We're the only error tracker that combines exception monitoring, uptime monitoring, and cron monitoring into a single, simple to use platform.

Our mission: to tame production and make you a better, more productive developer. Learn more

author photo

Josh Wood

Josh is Honeybadger's resident bug hunter and technical debt collector. Once enthusiastically referred to as a "human exception tracker", he spends his days crafting the middleware, plugins, and gems which keep the 'badger fat and happy through a steady diet of fresh data.

“We’ve looked at a lot of error management systems. Honeybadger is head and shoulders above the rest and somehow gets better with every new release.”
Michael Smith
Try Error Monitoring Free for 15 Days