PrettyErrors: Beautify Python Tracebacks for Clearer Debugging
PrettyErrors is a lightweight Python library that, with a single import, transforms raw exception tracebacks into neatly formatted, color‑coded outputs, making error locations and causes instantly recognizable, and offers configurable options for customizing colors, filenames, and enabling global usage via environment variables.
Even with Python, encountering errors can be frustrating; the raw traceback often looks like a tangled mess that makes you question both the code and yourself.
This article introduces PrettyErrors , a Python library that beautifies exception output, turning chaotic tracebacks into clean, color‑coded messages that highlight the error location and reason at a glance.
One line of code simplifies errors
Consider a simple script that raises a division‑by‑zero error:
<code>def foo():
1/0
foo()
</code>Without PrettyErrors, the traceback appears as a long, red‑filled block that is hard to read.
After importing pretty_errors , the output becomes concise and colorful, making the problem immediately obvious. The library also allows you to customize colors to suit your preferences.
When bugs are few, the difference may seem minor, but when an error fills an entire page, a clean, readable format can significantly reduce the psychological impact.
Installation and usage guide
PrettyErrors can be installed like any other third‑party Python package:
<code>python -m pip install pretty_errors
</code>To avoid importing the library in every script, you can enable it globally by running:
<code>python -m pretty_errors
</code>Once enabled, even SyntaxError messages are formatted nicely, which is not possible when the library is imported only within a script.
If you want to customize the appearance, PrettyErrors provides several configuration functions:
pretty_errors.configure()
pretty_errors.whitelist()
pretty_errors.blacklist()
pretty_errors.pathed_config()
For example, to change the filename color you can write:
<code>pretty_errors.configure(filename_color = pretty_errors.BRIGHT_YELLOW)
</code>If the changes seem to have no effect, ensure the environment variable PYTHON_PRETTY_ERRORS is set to 1 (a value of 0 disables the library):
<code>set PYTHON_PRETTY_ERRORS=1
</code>Remember that the terminal must support colored output for PrettyErrors to display colors; if you are using a monochrome terminal, you can adjust settings via pretty_errors.mono() .
In short, adding a few lines of configuration can make debugging less painful and more aesthetically pleasing.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.