Home >Backend Development >Python Tutorial >One line of code to make your Python bug unique
PrettyErrors is a tool for streamlining Python error messages. It is characterized by a very simple and friendly interface.
Its most significant function is to support color output in the terminal, mark out file stack traces, find error messages, filter out redundant information, extract key parts, and color mark them, thereby improving developers' s efficiency.
Writing code itself is not easy, especially if a bug appears in tens of thousands of lines of code, and you will not be able to find the problem for a while. At this time, you must be very crazy and irritable. Especially when there is an error in the Python code, the screen is full of error messages, making it even more difficult to locate the error.
Let’s take a look at the above traceback first
Is there a way to solve these problems?
Of course, in Python, there is no problem that a library cannot solve. Don’t mess with Python’s error output. One line of code can make the bug clearer.
The library we are going to introduce today is called pretty-errors. As you can tell from its name, its purpose is to beautify error messages.
The following describes how to install and use PrettyErrors.
With this command you can install it
linuxmi@linuxmi:~/www.linuxmi.com$ pip install pretty_errors
or
linuxmi@linuxmi:~/www.linuxmi.com$ pip3 install pretty-errors
if you want Each of your programs can make it clear and easy to read when reporting errors. Then run the following line of commands to configure it to be globally available.
linuxmi@linuxmi:~/www.linuxmi.com$ python3 -m pretty_errors
After the configuration is completed, if you run any script, the traceback will be automatically beautified.
Run this command also
linuxmi@linuxmi:~/www.linuxmi.com$ python3 -m pretty_errors
Enter C to clear the global configuration.
in a single file. After canceling global availability, you can import pretty_errors into the script file where you need to use pretty-errors according to your needs, and you can use
import pretty_errors
But in this way, the format of syntax error (SyntaxError) cannot be beautified. So in order to make the beautification more thorough, the official recommendation is that you use python -m pretty_errors
If you don’t like the default configuration, try these functions:
pretty_errors.configure() pretty_errors.whitelist() pretty_errors.blacklist() pretty_errors.pathed_config()
For example, if you want to change the color of the output file name, the code It’s like this:
pretty_errors.configure(filename_color = pretty_errors.BRIGHT_YELLOW)
If you find that nothing has changed after the above operations, then check PYTHON_PRETTY_ERRORS and treat it as When the value is 0, PrettyErrors are disabled.
set PYTHON_PRETTY_ERRORS = 1
It should be noted that the terminal you are using has a color output function, so the exception information output will have different colors. If you happen to be used to a monochrome terminal, you can try the settings in pretty_errors.mono().
from pretty_errors import * configure(filename_color=BRIGHT_BLUE)# 设置文件名为亮蓝色 def f(): return 1 / 0 if __name__ == "__main__": f()
In general, this library is very powerful and the effect is very cool. , it is the same as the PEP8 specification. It is okay without it, but it will be better with it. For some people who want to customize error output scenarios, pretty_errors can be a good solution.
The above is the detailed content of One line of code to make your Python bug unique. For more information, please follow other related articles on the PHP Chinese website!