PrettyErrors
PrettyErrors is a Python library designed to make Python exception output more legible and user-friendly. It provides color coding, simplifies tracebacks, and categorizes errors, making it easier for developers to understand what went wrong and how to fix it. The current version is 1.2.25. Its release cadence is irregular, with updates typically driven by new features or bug fixes rather than a strict schedule.
Common errors
-
PrettyErrors traceback appears, but without any colors, or output is garbled.
cause The terminal emulator does not support ANSI color codes, or `pretty-errors` is configured for color output in a monochrome environment.fixEnsure you are using a color-capable terminal (e.g., PowerShell, Cmder, VS Code terminal). If running in a monochrome environment, call `pretty_errors.mono()` after importing `pretty_errors` or configure colors manually to be empty strings. -
SyntaxError: invalid syntax (or similar syntax error) is not formatted by pretty-errors.
cause The `pretty-errors` library was imported manually within the script, or was not activated universally at Python startup.fixActivate `pretty-errors` universally by running `python -m pretty_errors` in your terminal once. This configures Python's startup procedure to include `pretty-errors`, allowing it to handle `SyntaxError` exceptions before your script even runs. -
PrettyErrors is not active when running my script, or standard Python tracebacks are shown.
cause The `pretty_errors` module was not imported or activated, or it was imported too late in the script after another library took over exception handling.fixEnsure `import pretty_errors` is one of the *very first* imports in your main script. Alternatively, for universal activation across all Python invocations, run `python -m pretty_errors` once to configure your Python environment.
Warnings
- gotcha PrettyErrors requires a terminal capable of color output to display colored exceptions effectively. On Windows, this means using PowerShell, Cmder, or similar; the standard `cmd.exe` may not render colors correctly.
- gotcha Manually importing `pretty_errors` (e.g., `import pretty_errors`) within your script will not format `SyntaxError` exceptions, as these occur before script execution. Only runtime exceptions will be prettified this way.
- gotcha PrettyErrors hooks into Python's exception handling system. This can potentially interfere with other libraries that also modify stack traces or exception reporting, leading to unexpected behavior or conflicts.
Install
-
pip install pretty_errors
Imports
- pretty_errors
import pretty_errors
Quickstart
import pretty_errors
# Optional: Configure pretty_errors for customized output
pretty_errors.configure(
separator_character = '#',
filename_display = pretty_errors.FILENAME_EXTENDED,
line_number_first = True,
display_link = True,
lines_before = 3,
lines_after = 2,
line_color = pretty_errors.RED + '> ' + pretty_errors.default_config.line_color,
code_color = ' ' + pretty_errors.default_config.line_color,
truncate_code = True,
display_locals = True
)
def calculate_division(numerator, denominator):
return numerator / denominator
# This will intentionally cause a ZeroDivisionError
print(calculate_division(10, 0))