Enhancing Python Error Messages with PrettyError
This article introduces the PrettyError library for Python, explains how to install it, demonstrates quick start examples, and details its key features such as colored output, timestamps, configurable stack traces, code context, and environment‑based settings to make debugging faster and more readable.
Programming often spends a large portion of time reading and fixing error messages, which can be verbose and hard to parse. The PrettyError library addresses these pain points by providing colorful, concise, and configurable error output.
1. Installation and Quick Start
Install PrettyError via pip: pip install pretty_errors Import the library and run a simple function to see the default error output:
import pretty_errors
def divide(a, b):
return a / b
divide(1, 0)After importing, the error is displayed with colored highlights, showing the exception type, file, and line.
2. Main Features
2.1 Configurable Colors
You can customize colors using the configure() function:
import pretty_errors
pretty_errors.configure(
line_color=pretty_errors.BRIGHT_RED,
exception_color=pretty_errors.BRIGHT_MAGENTA,
exception_arg_color=pretty_errors.CYAN,
exception_file_color=pretty_errors.RED_BACKGROUND + pretty_errors.BRIGHT_WHITE
)
open("non_existent_file.txt")2.2 Adding Timestamps
Enable timestamps in error output:
import pretty_errors
pretty_errors.configure(display_timestamp=1)
open("non_existent_file.txt")Or provide a custom timestamp function:
import pretty_errors, datetime
pretty_errors.configure(
display_timestamp=1,
timestamp_function=lambda: datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
)
open("non_existent_file.txt")2.3 Showing More Code Context
Use lines_before and lines_after to display surrounding code lines:
import pretty_errors
pretty_errors.configure(lines_before=2, lines_after=1)
# The output should show these comments (lines before)
def calculate(x):
return 1 / x
# The output should show this comment (lines after)
calculate(0)2.4 Controlling Stack Depth
Limit the displayed stack trace with stack_depth:
import pretty_errors
pretty_errors.configure(stack_depth=1)
def calculate(x):
return 1 / x
def wrapper():
calculate(0)
wrapper()2.5 Displaying Local Variables
Show variable values in the traceback by setting display_locals=1:
import pretty_errors
pretty_errors.configure(display_locals=1)
def calculate_divide(x, y):
return x / y
calculate_divide(1, 0)2.6 Environment‑Based Configuration
Adjust PrettyError settings based on an environment variable:
import pretty_errors, os
if os.getenv('ENV') == 'development':
pretty_errors.configure(stack_depth=0, display_locals=1)
else:
pretty_errors.configure(stack_depth=1, display_locals=0)
def calculate(x):
return 1 / x
def wrapper():
calculate(0)
wrapper()The article concludes that PrettyError simplifies Python error messages through color coding, timestamps, variable display, and flexible configuration, helping developers debug more efficiently.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
