Prettifies Python exception output to make it legible. Install it with
python -m pip install pretty_errors
If you want
pretty_errors to be used whenever you run a python script you must add it to your python startup procedure. You can do so easily by running:
python -m pretty_errors
This is the recommended way to use
pretty_errors; apart from being simpler and universal, using it will mean
SyntaxError exceptions also get formatted prettily (which doesn't work if you are manually importing
If you have not installed it universally you can use it in your project simply by importing it:
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 must use a monochrome terminal then you can call the helper function
pretty_errors.mono(), which will set some config options in a way that is useful for monochrome output.
If you want to configure the output then use
pretty_errors.pathed_config(). For example:
import pretty_errors pretty_errors.configure( separator_character = '*', filename_display = pretty_errors.FILENAME_EXTENDED, line_number_first = True, display_link = True, lines_before = 2, lines_after = 1, line_color = '> ' + pretty_errors.default_config.line_color, code_color = ' ' + pretty_errors.default_config.code_color, truncate_code = True, display_locals = True ) pretty_errors.blacklist('c:/python')
Whitelist / Blacklist:
You may use the functions
blacklist(path) to add paths which will be necessary (
whitelist) or excluded (
blacklist). The top frame of the stack is never excluded.
You may set up alternate configurations, which are triggered by the path to the code file of the frame. For example, if you were not interested in the system frames (those under 'c:/python') but did not want to hide them completely by using the
blacklist you could do this:
meh = pretty_errors.config.copy() meh.line_color = meh.code_color = meh.filename_color = meh.function_color = meh.line_number_color = ( pretty_errors.GREY ) pretty_errors.pathed_config(meh, 'c:/python')
You may temporarily disable
pretty_errors by setting the environment variable
0. i.e. at a command prompt:
pretty_errors.activate() will override this.
Configuration settings are stored in
pretty_errors.config, though should be set using
pretty_errors.configure(). A reference for the default config is stored in
Output will be wrapped at this point. If set to
0(which is the default) it will automatically match your console width.
Insert a hard newline even if the line is full. If
line_lengthis 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.
Character used to create the header line. Hyphen is used by default. If set to
''then header will be disabled.
When enabled a timestamp is written in the traceback header.
Function called to generate timestamp. Default is
When enabled the exception is displayed above the stack trace.
When enabled the exception is displayed below the stack trace.
The maximum number of entries from the stack trace to display. When
0will display the entire stack, which is the default.
When enabled the stack trace will be reversed, displaying the top of the stack first.
When enabled (which is the default) the bottom frame of the stack trace will always be displayed.
How the filename is displayed: may be
When enabled the line number will be displayed first, rather than the filename.
When enabled a link is written below the error location, which VSCode will allow you to click on.
How many lines of code to display for the top frame, before and after the line the exception occurred on.
How many lines of code to display for each other frame in the stack trace, before and after the line the exception occurred on.
When enabled each line of code will be truncated to fit the line length.
When enabled, local variables appearing in the top stack frame code will be displayed with their values.
When enabled, local variables appearing in other stack frame code will be displayed with their values.
When enabled the values of displayed local variables will be truncated to fit the line length.
When enabled an arrow will be displayed for syntax errors, pointing at the offending token.
Characters used to draw the arrow which points at syntax errors.
Text string which is displayed at the top of the report, just below the header.
Text string which is displayed between each frame of the stack.
Text string which is displayed at the bottom of the exception report.
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.
These color strings will be output before the relevant part of the exception message. You may include non-escape sequence strings if you wish; if you do not have a terminal which supports color output, or simply want to include extra demarcation.
Escape sequence to set header color.
Escape sequence to set timestamp color.
Escape sequence to set exception color.
Escape sequence to set exception arguments color.
Escape sequence to set filename color.
Escape sequence to set line number color.
Escape sequence to set function color.
Escape sequence to set link color.
Escape sequence to set the color of the line of code which caused the exception.
Escape sequence to set the color of other displayed lines of code.
Escape sequence to set the color of the arrow which points at syntax errors.
Escape sequence to set the color of the syntax error token.
Escape sequence to set the color of local variable names.
Escape sequence to set the color of local variable values.
Escape sequence to set the color of local value length when local is truncated.
pretty_errors has some built in escape sequence constants you can use when setting these colors:
For each color there is a matching
BRIGHT_ variant (i.e.
pretty_errors.BRIGHT_RED), as well as a
_BACKGROUND variant to set the background color (i.e.
pretty_errors.configure( line_color = pretty_errors.CYAN_BACKGROUND + pretty_errors.BRIGHT_WHITE )
For the most extensive customization (short of forking the package) you may override the default
ExceptionWriter class, allowing you to tailor the output however you wish. Typically you will only need to override the
class MyExceptionWriter(pretty_errors.ExceptionWriter): def write_header(self): self.output_text('######## ERROR ########') pretty_errors.exception_writer = MyExceptionWriter()
help(pretty_errors.ExceptionWriter) in the python interpreter for more details.