.. Starting to individual public number: Python programming time, please private letter reproduced
When we write a script or program of unpredictable occurrence of various abnormalities time if we do not capture process, usually resulting in a crash quit, and will print out a bunch in terminal dense of a stack traceback information to tell us, which went wrong.
Like this child, my God, have committed the trypophobia
Above this traceback
- Only two colors black and white, not like code highlighting that, to the naked eye to achieve a very unfriendly
- You can not directly display an error code to troubleshoot a step slow, inefficient
Is there a way that can solve these problems?
Yes, of course, in Python, a library is no problem can not be resolved, if there is, then wait for you to develop the library.
Today to introduce this library it is called pretty-errors
, from the name you can know its purpose, is used to beautify the error message.
Through this command you can install it
$ python3 -m pip install pretty-errors
1. Environmental Requirements
Due to the use pretty-errors
after your traceback information output, will have that kind of effect code highlighting, so when you use the test use pretty-error
, make sure that you use the terminal can be output with the font color.
On windows you can use Powershell, cmder etc.
On a Mac you can use the built-in terminal, or install a better use of iTerm2
2. The effect of contrast
Just write without using pretty-errors, and being given program is like this.
After the use of the pretty_errors, error message glorified in such a way.
Is not feeling a lot of clear, dense bring the kind of anxiety is not gone yet?
Of course, less code, you may not feel the effects of a project on Github's comparison chart would look at it
3. Configure globally available
After you can see the use of pretty_errors, nothing more than that filters out some interfere with our vision of useless information, then useful to us to highlight key information.
Since If so, then pretty_errors should also be able to support us how to customize what colors we use, how typography, right?
The answer is obvious.
pretty_errors and other libraries are not the same, to a certain extent (if you are using the global configuration of words), it is not out of the box, you may need to do some configuration before you use it.
Use this one command, and you will be configured, allowing traceback output when you run other scripts in that environment are automatically landscaping.
$ python3 -m pretty_errors
Once configured, you then run any script, traceback will automatically be beautified.
Not only in my iTerm terminal under
Also in the PyCharm
The only drawback is that the original traceback in PyCharm can click directly 文件路径
to jump directly to the corresponding error file line, and if you can use the following in VSCode custom configuration solution to this problem (will be mentioned below, the parameters are: display_link
).
Therefore, in some cases, you do not want to set pretty_errors
globally available.
That how to cancel the previous configuration?
Only output again python -m pretty_errors
, the output C
can be cleared.
4. The use of a single file
Cancel globally available, you can according to their needs, you need to use pretty-errors
a script file to import pretty_errors
, you can use
import pretty_errors
like this
import pretty_errors
def foo():
1/0
if __name__ == "__main__":
foo()
It is worth mentioning that, this way, if your script syntax errors, anomalies or in accordance with the output of the way before the show, and will not be beautified.
Therefore, in order to allow a more thorough landscaping, officially recommended that you use python -m pretty_errors
The custom settings
The example above, we are using pretty_errors
the default format beautify the information presented is not so full.
such as
- It does not show the absolute path of error file, which will allow us to be difficult to locate documents in which the code error.
- The specific error code is if we can, to show us on the terminal screen, and then we do not need the source code file to troubleshoot why.
If the pretty_errors
cause anomalies have lost, it might as well not use pretty_errors
it.
However, I can tell you is pretty_errors
not so easy as you think.
It is open enough, support for custom configuration, you can choose what information you need from the show, how to show?
Here's an example
import pretty_errors
# 【重点】进行配置
pretty_errors.configure(
separator_character = '*',
filename_display = pretty_errors.FILENAME_EXTENDED,
line_number_first = True,
display_link = True,
lines_before = 5,
lines_after = 2,
line_color = pretty_errors.RED + '> ' + pretty_errors.default_config.line_color,
code_color = ' ' + pretty_errors.default_config.line_color,
)
# 原来的代码
def foo():
1/0
if __name__ == "__main__":
foo()
In this way you use like the above pretty_errrs.configure
when configured, thrown exception information on the way things turned out.
Of course, pretty_errors.configure()
you can also receive a lot of parameters, you can configure according to your own needs.
5.1 Set Color
header_color
: Set the color of the header row.timestamp_color
: Set a time stamp colordefault_color
: Set the default colorfilename_color
: Set the file name colorline_number_color
: Set the color line number.function_color
: Color setting function.link_color
: Set the color of the link.
When setting the color, pretty_errors
it provides some common colors for your constant direct retrieval.
BLACK
:blackGREY
:grayRED
:redGREEN
:greenYELLOW
:yellowBLUE
:blueMAGENTA
: MagentaCYAN
:blue-greenWHITE
:white
And each color, are corresponding matching BRIGHT_
variants and _BACKGROUND
variants thereof,
Wherein, _BACKGROUND
for setting the background color, for example as follows.
5.2 Setting display contents
line_number_first
When enabled, line numbers will be displayed first, instead of the file name.lines_before
: Display first few lines of the occurrence of abnormality at thelines_after
: Displayed for a few lines of code at the occurrence of abnormalitydisplay_link
: When enabled, the link will be written under the wrong position, VScode will allow you to click the link.separator_character
: Used to create the title character line. Use a hyphen default. If set to''
orNone
, the title will be disabled.display_timestamp
: When enabled, the time stamp will be written back header.-
display_locals
When enabled, local variables and their values in the top of the stack frame code will be displayed. display_trace_locals
When enabled, the local variable stack frame other codes appearing in the displayed together with their values.
5.3 Setting how to display
-
line_length
: Length of each line is provided, the default is 0, meaning the output of each row will match the size of the console, if you set the length of a good match with the width of the console, it may need to be disabledfull_line_newline
, to prevent significant bis newline . -
full_line_newline
: When the output of the character row over whether to insert line breaks. -
timestamp_function
Call this function to generate a timestamp. The default valuetime.perf_counter
. -
top_first
When enabled, the stack trace will be reversed displayed first top of the stack. -
display_arrow
When enabled, it will show a syntax error for the arrow, point to a problem with the token. -
truncate_code
When enabled, each line will be truncated to fit the governor. -
stack_depth
The maximum number of entries to be displayed stack trace. What time0
will display the entire stack, which is the default. -
exception_above
When enabled, an exception will be displayed at the top of the stack trace. -
exception_below
:
When enabled, the tracking error display below the stack. -
reset_stdout
When enabled, reset escape sequence is written to stdout and stderr; if your console leave the wrong color, enable this option. -
filename_display
Display mode of the file name, there are three options:
pretty_errors.FILENAME_COMPACT
, ,pretty_errors.FILENAME_EXTENDED
orpretty_errors.FILENAME_FULL
The above is my pretty_errors
use of the experience, in general, this library is very powerful, the effect is particularly cool, it just PEP8 specification, as it is not possible, but it would be better with the same. For some people want to customize error output scenario, pretty_errors
would be a good solution, Domingo recommend it to you.