Tooling Tuesday: Pretty Errors in Python

Repeat after me, "Errors are good"

No seriously, errors are good, they give us information on what when wrong and where, but they don't look very nice. In fact sometimes they can scare the pants off us! Errors are there to alert us to an issue, but human beings see this alert as "You did something wrong puny human!!" and shut off.

So what is it?

pretty-errors is a library from Iain King to make exception output (errors) easier to understand.

Huh?



Here is a quick example, I wrote a quick project which has a for loop, but rather than an integer, which would control the for loop to go round a set number of times, I used a float which will throw the above error.



After installing pretty-errors, the above error message is made much more clear.

So how do I install it?

Via pip

On Linux / Mac

pip3 install pretty-errors

On Windows`

pip.exe install pretty-errors

So how do I use it?

This is the genius bit, all you need to do is import the library into your code and it just works. Here I've changed print to pint.

from time import sleep
import pretty_errors

for i in range(2):
    pint(i)

And here is the error.

Any limitations

Yeah, it only works in terminal / REPl that supports colours. So the IDLE REPL and Mu will not support it. But a normal terminal, or Visual Studio Code will work fine.

Also not all errors are covered, so syntax errors, such as While instead of while will not trigger a pretty error. :(

But this library looks useful and hopefully more errors will be added.

Happy Hacking