原生Python的traceback只有黑白配色,如果有报错信息,看起来非常不方便。而pretty_errors是一个可以对报错信息进行高亮的第三方库,大大提高工作效率。
安装非常简单,直接使用下面的命令即可。
pip install pretty_errors
单文件使用非常简单,直接在程序最开头添加如下代码即可。
import pretty_errors
默认配置已经比原始traceback好看很多,单仍然有一些缺点,比如终端中没有显示超链接。如果终端中可以显示超链接,那就可以用ctrl+鼠标左键,直接在vscode中定位到错误位置。
为了满足我们的需求,对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,
link_color = pretty_errors.BRIGHT_RED,
exception_color = pretty_errors.RED
)
具体的参数说明,可以在参考文章1中找到。
每次都导入这个库在进行配置非常的不方便,那有没有方法可以简化我们的工作,即无需手动导入,每次都自动完成呢?答案是肯定的
在终端输入下面的命令安装pretty_errors
,根据提示完成安装。一般来说,直接按回车选择默认配置即可。
python -m pretty_errors
当提示下面信息的时候,代表安装成功。
这行提示信息告诉我们,pretty_errors
这个库被添加到了usercustomize.py
中。至此安装完成了。
此时,我们不用手动引入该库,亦可享受traceback的高亮信息。
但是,还有一个问题。
那就是,自动导入使用默认配置,不能完全满足我们的需求。因此我们需要打开usercustomize.py
,将上面的配置信息写入该文件中。下面是我的usercustomize.py
文件,供诸君参考,同时也是我的备份。
### BEGIN PRETTY ERRORS
# pretty-errors package to make exception reports legible.
# v1.2.25 generated this config: newer version may have added methods/options not present!
try:
import pretty_errors
except ImportError:
print(
'You have uninstalled pretty_errors but it is still present in your python startup.' +
' Please remove its section from file:\n ' + __file__ + '\n'
)
else:
pass
# Use if you do not have a color terminal:
#pretty_errors.mono()
# Use if you are using a framework which is handling all the exceptions before pretty_errors can:
#if pretty_errors.active:
# pretty_errors.replace_stderr()
# Use to hide frames whose file begins with these paths:
#pretty_errors.blacklist('/path/to/blacklist', '/other/path/to/blacklist', ...)
# Use to only show frames whose file begins with these paths:
#pretty_errors.whitelist('/path/to/whitelist', '/other/path/to/whitelist', ...)
# Use to selectively set a config based on the path to the code of the current frame.
#alternate_config = pretty_errors.config.copy()
#pretty_errors.pathed_config(alternate_config, '/use/alternate/for/this/path')
# Use to configure output: Uncomment each line to change that setting.
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,
link_color = pretty_errors.BRIGHT_RED,
exception_color = pretty_errors.RED
)
"""pretty_errors.configure(
#always_display_bottom = True,
#arrow_head_character = '^',
#arrow_tail_character = '-',
#display_arrow = True,
#display_link = False,
#display_locals = False,
#display_timestamp = False,
#display_trace_locals = False,
#exception_above = False,
#exception_below = True,
#filename_display = pretty_errors.FILENAME_COMPACT, # FILENAME_EXTENDED | FILENAME_FULL,
#full_line_newline = False,
#infix = None,
#inner_exception_message = None,
#inner_exception_separator = False,
#line_length = 0,
#line_number_first = False,
#lines_after = 0,
#lines_before = 0,
#postfix = None,
#prefix = None,
#reset_stdout = False,
#separator_character = '-',
#show_suppressed = False,
#stack_depth = 0,
#timestamp_function = time.perf_counter,
#top_first = False,
#trace_lines_after = 0,
#trace_lines_before = 0,
#truncate_code = False,
#truncate_locals = True,
#arrow_head_color = '\x1b[1;32m',
#arrow_tail_color = '\x1b[1;32m',
#code_color = '\x1b[1;30m',
#exception_arg_color = '\x1b[1;33m',
#exception_color = '\x1b[1;31m',
#exception_file_color = '\x1b[1;35m',
#filename_color = '\x1b[1;36m',
#function_color = '\x1b[1;34m',
#header_color = '\x1b[1;30m',
#line_color = '\x1b[1;37m',
#line_number_color = '\x1b[1;32m',
#link_color = '\x1b[1;30m',
#local_len_color = '\x1b[1;30m',
#local_name_color = '\x1b[1;35m',
#local_value_color = '\x1b[m',
#syntax_error_color = '\x1b[1;32m',
#timestamp_color = '\x1b[1;30m',
name = "custom" # name it whatever you want
)"""
### END PRETTY ERRORS
usercustomize.py
2可以简单将该文件理解为python的开机自启动项。即每次用户调用Python的时候,会先执行该文件,因此当我们将pretty_errors
写入该文件中时,就无需手动调用了。
但是该文件仅仅在ENABLE_USER_SITE
为true的时候生效,该变量定义在site
中。因此当pretty_errors
不生效的时候,可以考虑一下是不是该变量为False
。
pretty_errors https://pypi.org/project/pretty-errors/1.1.8/ ↩︎
usercomtomize https://stackoverflow.com/questions/22177955/what-is-usercustomize ↩︎