Beautiful Error Messages in Python

Posted by

Pretty Error Messages in Python

Pretty Error Messages in Python

When programming in Python, error messages are a common occurrence. Whether you are a beginner or an experienced developer, dealing with error messages is an inevitable part of the coding process. However, Python allows you to customize and style your error messages to make them more visually appealing and easier to understand.

Using PrettyError

One way to create pretty error messages in Python is by using the PrettyError library. PrettyError is a Python package that allows you to customize the appearance and content of error messages. It provides a simple and elegant way to format and display errors in a visually appealing manner.


import pretty_errors

pretty_errors.configure(
    separator_character = '*',
    filename_display = pretty_errors.FILENAME_EXTENDED,
    line_number_first = False,
    display_link = True,
    display_locals = True,
    display_trace_lines = True,
    trace_line_exclude = ['raiseit', 'thepretty',
                         "_find_and_load",
                         "find_and_load",
                         "_handle_fromlist"
                     ],
    error_section = pretty_errors.PRETTY_ERRORS,
    line_number_color = pretty_errors.RED + pretty_errors.BOLD
)

def divide_by_zero():
    return 1 / 0

divide_by_zero()

Using the PrettyError library, you can customize various aspects of the error messages, such as the separator character, filename display, line number display, and more. This allows you to create error messages that are not only visually appealing but also provide valuable information for debugging purposes.

Styling with HTML

Another way to create pretty error messages in Python is by using HTML tags to style the error messages. By embedding HTML tags within the error message text, you can apply various styles such as font color, size, and formatting to make the error message stand out.


try:
    1/0
except ZeroDivisionError as e:
    print("

Error: Division by zero

")

In the example above, the error message is styled using HTML tags to display the error text in red and increase its font size. By incorporating HTML styling into your error messages, you can create visually pleasing and informative messages that are easy to understand.

Conclusion

In conclusion, by using libraries such as PrettyError and incorporating HTML styling into your error messages, you can create visually appealing and informative error messages in Python. These pretty error messages not only make the debugging process easier but also enhance the overall user experience when interacting with your code.

0 0 votes
Article Rating
17 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@IlyaCB
9 months ago

This one's so useful! Thank you!

@markhuckeby80
9 months ago

Another useful tip! Thank you!

@paulthomas1052
9 months ago

Very useful package – great demo – thanks.

@Little-bird-told-me
9 months ago

good job

@kenjaev_hasan
9 months ago

Thanks brother.

@andiglazkov4915
9 months ago

Thanks a lot!

@rip4real437
9 months ago

great video as always man

@bresent
9 months ago

Programmers back then: I wrote a 2-pass assembler in binary in 1024, 15 bit words and toggled it in through the front panel switches

Programmers now: Ew! the errors look so ugly ! Lets make them prettier

@akshat_tamrakar
9 months ago

🆒

@lucifer7456
9 months ago

Set the speed to 2.0 and wonderd how is he coding so fucking fast. Great introduction to the package, seems of high usability!

@joswadsouza1445
9 months ago

Such a informative

@Indently
9 months ago

My code might not run, but at least the error messages will look pretty

@philtoa334
9 months ago

Thx_.

@thepurplesmurf
9 months ago

This module is a trap that slows down your workflow. Once installed you want to produce errors intentionally because you want to look at the pretty error messages instead of focusing on writing error free code. 😁

@diegoahumada4107
9 months ago

Good video, as always. Greetings from Argentina 👋

@kindder1238
9 months ago

this guy saving my computer science project

@Termux_Earn_Money
9 months ago

Very good bro

Get me your telegram id