{"id":5862,"library":"better-exceptions","title":"Better Exceptions","description":"Better Exceptions is a Python library that automatically enhances standard Python traceback outputs, making them significantly more readable and useful for debugging. It achieves this through features like colorization, displaying more context around code lines, and printing relevant variable values at each point in the traceback. The current version is 0.3.3. It has a slow release cadence, with the last significant update (0.2.1) being several years ago, focusing on stability.","status":"maintenance","version":"0.3.3","language":"en","source_language":"en","source_url":"https://github.com/qix-/better-exceptions","tags":["debugging","exceptions","traceback","development-tool"],"install":[{"cmd":"pip install better_exceptions","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"note":"While 'import better_exceptions' is still valid for manual configuration or to call `hook()`, automatic activation in versions 0.2.1 and newer primarily relies on the BETTER_EXCEPTIONS environment variable. Simply importing might not activate the hook globally without the environment variable or an explicit `better_exceptions.hook()` call.","wrong":"import better_exceptions # without setting BETTER_EXCEPTIONS=1 or calling hook() in v0.2.1+","symbol":"better_exceptions","correct":"import better_exceptions"}],"quickstart":{"code":"import os\n\nos.environ['BETTER_EXCEPTIONS'] = '1'\n\ndef divide_by_zero():\n    a = 10\n    b = 0\n    return a / b\n\nif __name__ == '__main__':\n    try:\n        divide_by_zero()\n    except ZeroDivisionError:\n        print(\"\\n--- Better Exceptions Output (if enabled) ---\")\n        # The traceback will be formatted by better-exceptions if the hook is active.\n        # This print statement is just to show where the formatted output would appear.\n        raise # Re-raise to show the formatted traceback","lang":"python","description":"To quickly enable better-exceptions, set the `BETTER_EXCEPTIONS` environment variable to '1' and then run your Python script. The library will automatically hook into the exception handling system and format any uncaught exceptions."},"warnings":[{"fix":"Ensure `export BETTER_EXCEPTIONS=1` (or `setx BETTER_EXCEPTIONS 1` on Windows) is set in your environment before running Python. Alternatively, call `better_exceptions.hook()` after importing in your script for explicit activation.","message":"Activation mechanism changed in v0.2.1. Previously, `import better_exceptions` was often sufficient for automatic activation. From v0.2.1 onwards, the primary method for automatic, global activation is setting the `BETTER_EXCEPTIONS=1` environment variable. Simply importing the library in newer versions may not activate the enhanced tracebacks without this environment variable or an explicit `better_exceptions.hook()` call.","severity":"breaking","affected_versions":">=0.2.1"},{"fix":"Always unset the `BETTER_EXCEPTIONS` environment variable or remove `better_exceptions.hook()` calls when deploying to production.","message":"Do not use `better-exceptions` in production environments. It prints detailed local variable values within tracebacks, which can leak sensitive data into logs or error reports. This is a security risk in a production setting.","severity":"gotcha","affected_versions":"All"},{"fix":"If `better-exceptions` doesn't seem to be working, check for other installed packages or system configurations that might be overriding `sys.excepthook`. Uninstall or disable conflicting components if possible.","message":"Conflicts with other exception handlers. `better-exceptions` works by replacing `sys.excepthook`. Other libraries or system tools (e.g., Ubuntu's `python3-apport`) that also modify `sys.excepthook` can interfere, preventing `better-exceptions` from functioning correctly or vice-versa.","severity":"gotcha","affected_versions":"All"},{"fix":"For persistent activation across sessions on Linux/OSX, add `export BETTER_EXCEPTIONS=1` to your shell's configuration file (e.g., `~/.profile`, `~/.bashrc`, or `~/.zshrc`). On Windows, use `setx BETTER_EXCEPTIONS 1` for a persistent system-wide variable.","message":"Environment variable `BETTER_EXCEPTIONS=1` is not persistent by default on Linux/OSX. Using `export` only sets it for the current terminal session.","severity":"gotcha","affected_versions":"All"},{"fix":"Always verify the context of search results to ensure they refer to the Python library `better-exceptions` by checking the domain (e.g., GitHub for the Python project, PyPI) or content specific to Python development.","message":"There is a popular Sims 4 game mod also named 'Better Exceptions'. When searching for troubleshooting or usage information, ensure you are referencing documentation for the Python library and not the game mod, as they are entirely unrelated projects.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}