better-exceptions

Pretty and useful exceptions in Python, automatically.

  • Owner: Qix-/better-exceptions
  • Platform:
  • License:: MIT License
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

better-exceptions Travis

Pretty and more helpful exceptions in Python, automatically.

Example screenshot of exceptions

Usage

Install better_exceptions via pip:

$ pip install better_exceptions

And set the BETTER_EXCEPTIONS environment variable to any value:

export BETTER_EXCEPTIONS=1  # Linux / OSX
setx BETTER_EXCEPTIONS 1    # Windows

That's it!

Python REPL (Interactive Shell)

In order to use better_exceptions in the Python REPL, first install the package (as instructed above) and run:

$ python -m better_exceptions
Type "help", "copyright", "credits" or "license" for more information.
(BetterExceptionsConsole)
>>>

in order to drop into a better_exceptions-enabled Python interactive shell.

Advanced Usage

If you want to allow the entirety of values to be outputted instead of being truncated to a certain amount of characters:

import better_exceptions
better_exceptions.MAX_LENGTH = None

While using better_exceptions in production, do not forget to unset the BETTER_EXCEPTIONS variable to avoid leaking sensitive data in your logs.

Use with unittest

If you want to use better_exceptions to format unittest's exception output, you can use the monkey patch below:

import sys
import unittest
import better_exceptions

def patch(self, err, test):
    lines = better_exceptions.format_exception(*err)
    if sys.version_info[0] == 2:
        return u"".join(lines).encode("utf-8")
    return "".join(lines)

unittest.result.TestResult._exc_info_to_string = patch

Note that this uses an undocumented method override, so it is not guaranteed to work on all platforms or versions of Python.

Django Usage

Tested with Django 1.11

Create a middleware exception handler in a file like myapp/middleware.py:

import sys
from better_exceptions import excepthook


class BetterExceptionsMiddleware(object):
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        return self.get_response(request)

    def process_exception(self, request, exception):
        excepthook(exception.__class__, exception, sys.exc_info()[2])
        return None

In settings.py, add your new class to the MIDDLEWARE list:

MIDDLEWARE = [
...
    'myapp.middleware.BetterExceptionsMiddleware',
]

example output:

image

Troubleshooting

If you do not see beautiful exceptions, first make sure that the environment variable does exist. You can try echo $BETTER_EXCEPTIONS (Linux / OSX) or echo %BETTER_EXCEPTIONS% (Windows). On Linux and OSX, the export command does not add the variable permanently, you will probably need to edit the ~/.profile file to make it persistent. On Windows, you need to open a new terminal after the setx command.

Check that there is no conflict with another library, and that the sys.excepthook function has been correctly replaced with the better_exceptions's one. Sometimes other components can set up their own exception handlers, such as the python3-apport Ubuntu package that you may need to uninstall.

Make sure that you have not inadvertently deleted the better_exceptions_hook.pth file that should be in the same place as the better_exceptions folder where all of your Python packages are installed. Otherwise, try re-installing better_exceptions.

You can also try to manually activate the hook by adding import better_exceptions; better_exceptions.hook() at the beginning of your script.

Finally, if you still can not get this module to work, open a new issue by describing your problem precisely and detailing your configuration (Python and better_exceptions versions, OS, code snippet, interpeter, etc.) so that we can reproduce the bug you are experiencing.

License

Copyright © 2017, Josh Junon. Licensed under the MIT license.

Main metrics

Overview
Name With OwnerQix-/better-exceptions
Primary LanguagePython
Program languagePython (Language Count: 2)
Platform
License:MIT License
所有者活动
Created At2017-03-12 11:23:04
Pushed At2023-03-23 11:09:43
Last Commit At2023-01-05 01:12:47
Release Count17
Last Release Name0.3.3 (Posted on 2021-01-29 17:48:18)
First Release Name0.1.0 (Posted on 2017-03-12 04:25:57)
用户参与
Stargazers Count4.7k
Watchers Count66
Fork Count203
Commits Count102
Has Issues Enabled
Issues Count78
Issue Open Count31
Pull Requests Count36
Pull Requests Open Count7
Pull Requests Close Count13
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private