Prettifies Python exception output to make it legible. Install it with
python -m pip install pretty-errors
Use it simply by importing it:
import pretty_errors
Note you need to be running in a terminal capable of colour output in order to get colour output: in Windows this means powershell, cmder, etc.
If you want to configure the output then use pretty_errors.configure()
. For example:
import pretty_errors
pretty_errors.configure(
seperator_character = '*',
filename_display = pretty_errors.FILENAME_EXTENDED,
line_number_first = True,
display_link = True,
lines_before = 2,
lines_after = 1,
line_prefix_color = '\033[1;33m',
line_prefix = '> ',
code_prefix = ' ',
truncate_code = True,
display_locals = True
)
pretty_errors.blacklist('c:/python')
If you want pretty-errors
to be run whenever you run a python program, without having to import
it manually, then you can add it to your sitecustomize.py
:
- Run
python -m site
and look for theUSER_SITE
entry. - In that folder create file
sitecustomize.py
, if it does not exist. - Add
import pretty_errors
to that file, plus whatever config calls you want.
You may use the functions whitelist(path)
and blacklist(path)
to add paths which will be necessary (whitelist
) or excluded (blacklist
). The top frame of the stack is never excluded.
-
always_display_bottom
When enabled (which is the default) the bottom frame of the stack trace will always be displayed. -
code_prefix
String displayed before each line of context code. -
display_link
When enabled a link is written below the error location, which VSCode will allow you to click on. -
display_locals
When enabled, local variables appearing in the top stack frame code will be displayed with their values. -
display_timestamp
When enabled a timestamp is written in the traceback header. -
display_trace_locals
When enabled, local variables appearing in other stack frame code will be displayed with their values. -
exception_above
When enabled the exception is displayed above the stack trace. -
exception_below
When enabled the exception is displayed below the stack trace. -
filename_display
How the filename is displayed: may bepretty_errors.FILENAME_COMPACT
,pretty_errors.FILENAME_EXTENDED
, orpretty_errors.FILENAME_FULL
-
full_line_newline
Insert a hard newline even if the line is full. Ifline_length
is the same as your console width and this is enabled then you will see double newlines where unwanted, so usually you would only set this if they are different. -
infix
Text string which is displayed between each frame of the stack. -
line_length
Output will be wrapped at this point. If set to0
(which is the default) it will automatically match your console width. -
line_number_first
When enabled the line number will be displayed first, rather than the filename. -
line_prefix
String displayed before the line which caused the exception. -
lines_after
,lines_before
How many lines of code to display for the top frame, before and after the line the exception occurred on. -
postfix
Text string which is displayed at the bottom of the exception report. -
prefix
Text string which is displayed at the top of the report, just below the header. -
reset_stdout
When enabled the reset escape sequence will be written to stdout as well as stderr; turn this on if your console is being left with the wrong color. -
seperator_character
Character used to create the header line. Hyphen is used by default. -
stack_depth
The maximum number of entries from the stack trace to display. When0
will display the entire stack, which is the default. -
top_first
When enabled the stack trace will be reversed, displaying the top of the stack first. -
trace_lines_after
,trace_lines_before
How many lines of code to display for each other frame in the stack trace, before and after the line the exception occurred on. -
truncate_code
When enabled each line of code will be truncated to fit the line length. -
truncate_locals
When enabled the values of displayed local variables will be truncated to fit the line length.
-
code_prefix_color
Escape sequence to set color of prefix for context code lines. -
exception_arg_color
Escape sequence to set exception arguments color. -
exception_color
Escape sequence to set exception color. -
filename_color
Escape sequence to set filename color. -
function_color
Escape sequence to set function color. -
header_color
Escape sequence to set header color. -
link_color
Escape sequence to set link color. -
line_number_color
Escape sequence to set line number color. -
line_color
Escape sequence to set the color of the line of code which caused the exception. -
line_prefix_color
Escape sequence to set color of prefix for line of code which caused the exception. -
code_color
Escape sequence to set the color of other displayed lines of code. -
local_len_color
Escape sequence to set the color of local value length when local is truncated. -
local_name_color
Escape sequence to set the color of local variable names. -
local_value_color
Escape sequence to set the color of local variable values. -
timestamp_color
Escape sequence to set timestamp color.