This is my ninth day of the August Genwen Challenge

Even in Python, error reporting can be confusing. Do you come across it often?

Well, here it is today

PrettyErrors ¶ (Use this or TA for the rest of the term)

With only one import, errors can be reported neatly, and the location of the error code and the reason for the error can be clearly seen at a glance.

One line of code simplification error

Let’s try a simple mistake

def foo() :
    1/0
foo()
Copy the code

Before you stop using it, the error message looks like this:

Let’s import pretty_errors. What would the output look like?

Isn’t it a lot clearer and cleaner? Of course, it can also customize colors.

I don’t think there is a problem when there is less wrong information.

When the error message spread a whole page, beautiful or not beautiful, the degree of mental endurance will be different.

user’s guidance

Like all third party libraries for Python, TA is simple to install.

python -m pip install pretty_errors
Copy the code

If you want each of your programs to be able to maintain their beauty when reporting errors, run the following command instead of importing TA every time.

python -m pretty_errors
Copy the code

And SyntaxError formatting can be beautified as well. This is not possible when you simply import pretty_errors from your program.

To create their own satisfactory beautification effect, then try these functions:

pretty_errors.configure()
pretty_errors.whitelist()
pretty_errors.blacklist()
pretty_errors.pathed_config()
Copy the code

For example, to change the color of the output file name, the code looks like this:

pretty_errors.configure(filename_color = pretty_errors.BRIGHT_YELLOW)
Copy the code

If you find that nothing has changed since PYTHON_PRETTY_ERRORS, check that PYTHON_PRETTY_ERRORS is disabled when PYTHON_PRETTY_ERRORS is 0.

set PYTHON_PRETTY_ERRORS=1
Copy the code

It is important to note that the terminal you are using has a color output function, so the output of exception messages will have a different color.

If you unfortunately use a monochrome terminal, try pretty_errors.mono(). Life is short and there are lots of bugs. Why not try TA and add some beauty to the Debug process?

That’s the end of this article, thanks for watching, next in our Python Tips series on adding a progress bar to an application

To thank you readers, I’d like to share some of my recent programming favorites to give back to each and every one of you in the hope that they can help you.

Dry goods mainly include:

① Over 2000 Python ebooks (both mainstream and classic books should be available)

②Python Standard Library (Most Complete Chinese version)

③ project source code (forty or fifty interesting and classic practice projects and source code)

④Python basic introduction, crawler, Web development, big data analysis video (suitable for small white learning)

⑤ A Roadmap for Learning Python

All done ~ see personal profile or private letter for complete source code.

Review past

This move lets your PIP download speed take off