pretty-errors:美化python异常输出以使其清晰易读

 

1. 安装pretty-errorspython

python -m pip install pretty_errors

2.若是你想让你的每个程序都能这样在报错时也保持美貌,那么运行下面这这行命令,就不用每次都 import pretty_errors 。这是使用pretty_errors的推荐方法;除了更简单和通用以外,使用它意味着SyntaxError异常也会获得prettly格式化(若是手动导入pretty_errors,则这不起做用)。git

python -m pretty_errors

若是您尚未安装它,只需导入它,就能够在项目中使用它:github

import pretty_errors

注意:要得到颜色输出,您须要在可以输出颜色的终端中运行:在Windows中,这意味着powershell、cmder等。若是必须使用单色终端,则能够调用助手函数pretty_errors.mono(),该函数将以对单色输出有用的方式设置一些配置选项。shell

若是要配置输出,请使用pretty_errors.configure()、pretty_errors.whitelist()、pretty_errors.blacklist()、pretty_errors.pathed_config()。例如:函数

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, truncate_code = True, display_locals = True ) pretty_errors.blacklist('c:/python')

3.经过将环境变量PYTHON_prety_errors设置为0,能够暂时禁用prety_errors。url

set PYTHON_PRETTY_ERRORS=0spa

调用pretty_errors.activate()将覆盖此项。.net

 

详见:https://github.com/onelivesleft/PrettyErrorscode

相关文章
相关标签/搜索