One Line of Code to Say Goodbye to Confusing Python Error Messages

Author:Murphy  |  View: 26232  |  Time: 2025-03-22 22:39:24
Image by 15548337 from Pixabay

Programming is such an activity that we may use 20% of our time to write the ideas into code and then 80% to clear the errors and fix the bugs. Error messages are definitely something we will see every day. However, have you experienced any difficulties with Python Error messages?

For example, the error messages can be very verbose, which is not bad, but it is difficult to distinguish different parts and quickly find the information we need. The stack traces are also sometimes too overwhelming and complex to understand. It is also not easy to customise the error message unless we override the Exception classes, which could be overwhelming again.

In this article, I'm going to introduce a library called PrettyError that can help us address all of the pain points mentioned above and more. It has many cool features that can streamline our debugging process and help us save lots of time during the coding jobs.

1. Installation & Quick Start

Image by Pexels from Pixabay

As usual, installing the Pretty Error library is quite easy. We just simply run pip to obtain it from PyPI.

pip install pretty_errors

A Quick Start

This is probably the quickest quick start guide. When we want to use the library with its default configuration, all we need to do is to import it before coding.

Python">import pretty_errors

Now, let's define a function without try-except so that later on we can manually create some errors.

def divide(a, b):
    return a / b

Then, let's first see what it looks like without Pretty Errors. We will simulate a division by zero error.

divide(1, 0)

That is the original error message in Python. Then, let's enable Pretty Error by simply importing it, and running the code again.

import pretty_errors

divide(1, 0)

It's already colour-coded and comes with simplified indicators such as the stdin and the function name divide.

Because of the limitation of this blog article, it's not practical to demo how the colour code will be useful in practice. There is a screenshot from the GitHub README, as shown below.

Image Courtesy: https://raw.githubusercontent.com/onelivesleft/PrettyErrors/master/example.png

In the above scenario, do you like the one on the left (original) or on the right (Pretty Error)?

Tags: Artificial Intelligence Programming Python Software Development Technology

Comment