Library / SDK
Qix-/better-exceptions avatar
Qix-/better-exceptions

better-exceptions: pretty tracebacks switched on by an environment variable

Pretty and useful exceptions in Python, automatically.

4,709 stars231 forksPythonMIT

At a glance

What is it?
better-exceptions replaces Python's default exception output without asking you to import anything: you install it, set BETTER_EXCEPTIONS to any value, and a .pth file in site-packages installs a sys.excepthook for you. That automation is also its main operational hazard, because the same hook that prints your objects will print whatever those objects contain, and the README tells you to unset the variable in production.
Who is it for?
Adopt better-exceptions on developer machines and CI, where printing full object state on failure is exactly what you want and the truncation limit is worth raising. Do not leave it enabled in production, which the README says plainly because the hook writes variable contents into your logs.
Can I use it commercially?
Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
No. The owners have archived the repository on GitHub, so it is read-only and no longer receives changes.
What is it written in?
Mainly Python, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on November 22, 2026, and from our analysis. They are not legal advice.

From daily snapshots

Star history

  • Qix-/better-exceptions4,709 ★ · -9
Star history
Repository2026-09-232026-11-22
Qix-/better-exceptions4.7k4.7k

-9 Stars in the last 60 days, measured from the daily GitHub snapshot. Compare with other projects

Editorial analysis

Activation is an environment variable and a .pth file

There is nothing to import. The whole installation is one command:

console
pip install better_exceptions

followed by setting the variable to any value:

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

What makes that work is packaging rather than convention. The repository root holds `better_exceptions_hook.pth`, and `setup.py` defines four custom setup commands, `BuildWithPTH`, `EasyInstallWithPTH`, `InstallLibWithPTH` and `DevelopWithPTH`, each of which copies that file into the build or install directory next to the packages. A `.pth` file is executed by the interpreter at startup, which is how the hook gets registered without your code importing anything. The same file also shows where the version comes from: `setup.py` reads `__version__` out of `better_exceptions/__init__.py` with a regular expression rather than declaring it in the manifest.

The hook replaces sys.excepthook, so conflicts are the failure mode

The troubleshooting section names the actual mechanism, which matters when nothing happens. Check that there is no conflict with another library, and that the `sys.excepthook` function has been correctly replaced with the better_exceptions one. Sometimes other components set up their own exception handlers, and the example given is the `python3-apport` Ubuntu package, which you may need to uninstall. That is the whole class of problem here: two things want the same global hook, the last one installed wins, and the symptom is a traceback that looks completely standard. There is a manual escape hatch for exactly this situation, adding `import better_exceptions; better_exceptions.hook()` at the beginning of your script, which forces the hook at a point you control rather than at interpreter startup.

MAX_LENGTH is the one knob, and it truncates by default

Out of the box the formatter cuts long values off at a fixed number of characters, which is the right default for a traceback you want to read quickly and the wrong one when the value is the thing you are debugging. The advanced section gives you the whole change:

python
import better_exceptions
better_exceptions.MAX_LENGTH = None

Setting it to `None` allows the entirety of values to be output instead of being truncated. There is one attribute and no other documented configuration in the README, which tells you how narrow the surface is. Everything else you would want to tune, the expression rendering, the syntax colouring, the value formatting, is left at whatever the library decides, so if a traceback reads well but the parts you rely on are missing, that attribute is the only dial described.

Production means unsetting the variable

The README is direct about it: while using better_exceptions in production, do not forget to unset the BETTER_EXCEPTIONS variable to avoid leaking sensitive data in your logs. That follows from what the library does, which is print the contents of the objects in the failing frame, and from `MAX_LENGTH = None` being one line away. Persistence is the other half of the operational story, and it differs by platform. On Linux and macOS the `export` command does not add the variable permanently, so you will probably need to edit `~/.profile` to make it stick, which is exactly where you do not want it once the code reaches a server. On Windows `setx` writes it for the future, and you need to open a new terminal after running it, so the change is invisible in the shell you typed it into.

The unittest integration overrides a private method

Formatting unittest output takes a monkey patch, and the README is honest about the fragility:

python
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

The warning under it is the important sentence: this uses an undocumented method override, so it is not guaranteed to work on all platforms or versions of Python. You are replacing `_exc_info_to_string` on the result class, a name unittest does not promise to keep, and the code still carries a Python 2 branch that encodes to utf-8, which dates the snippet. If you adopt this, pin the interpreter and re-check it on every unittest upgrade.

Django needs a middleware entry and a logging filter

The Django path touches two places in a project. First the middleware list:

python
MIDDLEWARE = [
    # ...
    "better_exceptions.integrations.django.BetterExceptionsMiddleware",
]

Then the logging configuration, which is the part with a caveat. You import `skip_errors_filter` from `better_exceptions.integrations.django`, and the README notes that if you would rather not override `LOGGING` wholesale you can vendor Django's default logging configuration and update it, with the default for Django 3.1.4 linked as the starting point. In other words the library expects to own your logging config unless you copy the framework's defaults by hand, which is a decision to make before the first exception rather than after.

The last release is from 2018 and the branch moved in 2026

The release history explains part of the risk. The tags are 0.1.8 on 2017-05-12 and 0.2.1 on 2018-01-16, and the last recorded push to the default branch is dated 2026-10-08, and the repository is now archived; the author's README warning says he could not release a new version to PyPI if he wanted to, that the module was never meant to be used in production, and that forks appearing on PyPI may carry malware. If you install by name today you get 0.2.1, not the current branch, and that gap is the first thing to weigh. The rest of the tree is small and conventional: `better_exceptions/` and its `integrations` package, `test/` plus a `test_all.sh` runner at the root, `setup.cfg` and `MANIFEST.in` for packaging, an `.editorconfig`, and a `screenshot.png` that shows the output the project is selling. Licence is MIT, copyright Josh Junon, 2017.

Editorial conclusion

Adopt better-exceptions on developer machines and CI, where printing full object state on failure is exactly what you want and the truncation limit is worth raising. Do not leave it enabled in production, which the README says plainly because the hook writes variable contents into your logs. Verify first that nothing else on the host has already claimed `sys.excepthook`, that the `better_exceptions_hook.pth` file survived installation beside your packages, and that your interpreter is one the packaging in `setup.py` still supports, since the pinned release here is 0.2.1 from 2018.

Frequently asked questions

How do I install and enable better-exceptions?

Run pip install better_exceptions, then set BETTER_EXCEPTIONS to any value: export BETTER_EXCEPTIONS=1 on Linux or macOS, setx BETTER_EXCEPTIONS 1 on Windows. There is no import to add, because the package installs better_exceptions_hook.pth which registers the hook for you.

Why are my better-exceptions not being formatted?

First confirm the variable exists with echo $BETTER_EXCEPTIONS on Linux or macOS and echo %BETTER_EXCEPTIONS% on Windows, then check that sys.excepthook was replaced and that no other component installed its own handler, such as the python3-apport Ubuntu package. Finally confirm better_exceptions_hook.pth still sits beside the better_exceptions folder in site-packages.

Can I leave better-exceptions enabled in production?

The README says not to: unset BETTER_EXCEPTIONS while using it in production to avoid leaking sensitive data in your logs. Remember that export does not persist on Linux or macOS unless you edit ~/.profile, while setx on Windows writes the value for future terminals.

How do I stop better-exceptions truncating long values?

Set the module attribute to None: import better_exceptions, then better_exceptions.MAX_LENGTH = None. That allows the entirety of values to be output instead of being truncated to a certain number of characters.

Does better-exceptions work with unittest and Django?

For unittest the README shows a monkey patch replacing TestResult._exc_info_to_string with a call to better_exceptions.format_exception, and warns that it relies on an undocumented method override. For Django you add BetterExceptionsMiddleware to MIDDLEWARE and update the logging configuration using skip_errors_filter.

Official sources

  1. Issues
  2. License: MIT
  3. Qix-/better-exceptions on GitHub
  4. README
  5. Releases
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/qix-better-exceptions.svg)](https://hysenlabs.com/projects/qix-better-exceptions)

Star history chart for your README

markdown
[![Star history](https://hysenlabs.com/chart/qix-better-exceptions.svg)](https://hysenlabs.com/projects/qix-better-exceptions)

Was this useful?