Package: pretty-errors
pretty-errorsPrettifies Python exception output to make it legible. Install it withpython-mpipinstallpretty_errorsIf you wantpretty_errorsto 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-mpretty_errorsThis is the recommended way to usepretty_errors; apart from being simpler and universal, using it will meanSyntaxErrorexceptions also get formatted prettily (which doesn't work if you are manually importingpretty_errors).If you have not installed it universally you can use it in your project simply by importing it:importpretty_errorsNote 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 functionpretty_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 usepretty_errors.configure(),pretty_errors.whitelist(),pretty_errors.blacklist(),pretty_errors.pathed_config().  For example:importpretty_errorspretty_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')Scraping STDERRSometimes it will be impossible forpretty_errorsto utilizesys.excepthook: for instance, if you are using a framework which installs its own logging (such asuvicorn).  In such cases, you can makepretty_errorsscrape the output tostderrinstead, replacing it with its own.  To do so simple call:pretty_errors.replace_stderr()Note that this will lose some functionality, sincepretty_errorswill only have access to what is being output on screen, rather then the entire stack trace.  A good API will generally have a way to interact with the exception stack, which will allow for usingexcepthook:replace_stderrshould be the last resort.See this comment for an exampleWhitelist / Blacklist:You may use the functionswhitelist(path)andblacklist(path)to add paths which will be necessary (whitelist) or excluded (blacklist).  The top frame of the stack is never excluded.Pathed ConfigurationsYou 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 theblacklistyou 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')Environment VariablesPYTHON_PRETTY_ERRORSYou may disablepretty_errorsby setting the environment variablePYTHON_PRETTY_ERRORSto0.  i.e. at a command prompt:setPYTHON_PRETTY_ERRORS=0Callingpretty_errors.activate()will override this.If you wish to selectively utilizepretty_errors, then use the above, and in your code perform your calculation to determine whether or not to callpretty_errors.activate().PYTHON_PRETTY_ERRORS_ISATTY_ONLYIt may be desirable to disablepretty_errorswhen you are redirecting output to a file (to keep error logs, for instance).  If you wish to do so, then settingPYTHON_PRETTY_ERRORS_ISATTY_ONLYto non-zero will causepretty_errorsto check if it is running in an interactive terminal, and only activate if so.setPYTHON_PRETTY_ERRORS_ISATTY_ONLY=1Setting this will disablereplace_stderr()in the same situations, unless you call it with theforceparameter:replace_stderr(force=True).Callingpretty_errors.activate()will override this.You may checkpretty_errors.terminal_is_interactiveto see if the terminal is interactive (pretty_errorssets this by checkingsys.stderr.isatty()).  You can use this to select a different config.  For example:ifnotpretty_errors.terminal_is_interactive:pretty_errors.mono()Configuration settings:Configuration settings are stored inpretty_errors.config, though should be set usingpretty_errors.configure().  A reference for the default config is stored inpretty_errors.default_config.nameOptional field to store config name in.line_lengthOutput will be wrapped at this point.  If set to0(which is the default) it will automatically match your console width.full_line_newlineInsert a hard newline even if the line is full.  Ifline_lengthis the same as your console width and this is enabled then you will see double newlines when the line is exactly full, so usually you would only set this if they are different.separator_characterCharacter used to create the header line.  Hyphen is used by default.  If set toNoneor''then header will be disabled.display_timestampWhen enabled a timestamp is written in the traceback header.timestamp_functionFunction called to generate timestamp.  Default istime.perf_counter.exception_aboveWhen enabled the exception is displayed above the stack trace.exception_belowWhen enabled the exception is displayed below the stack trace.stack_depthThe maximum number of entries from the stack trace to display.  When0will display the entire stack, which is the default.top_firstWhen enabled the stack trace will be reversed, displaying the top of the stack first.always_display_bottomWhen enabled (which is the default) the bottom frame of the stack trace will always be displayed.show_suppressedWhen enabled all suppressed exceptions in the stack trace will be shown (typically they are suppressed because an exception above them has replaced them).  The normal python behaviour is to hide them.filename_displayHow the filename is displayed: may bepretty_errors.FILENAME_COMPACT,pretty_errors.FILENAME_EXTENDED, orpretty_errors.FILENAME_FULLline_number_firstWhen enabled the line number will be displayed first, rather than the filename.display_linkWhen enabled a link is written below the error location, which VSCode will allow you to click on.lines_after,lines_beforeHow many lines of code to display for the top frame, before and after the line the exception occurred on.trace_lines_after,trace_lines_beforeHow many lines of code to display for each other frame in the stack trace, before and after the line the exception occurred on.truncate_codeWhen enabled each line of code will be truncated to fit the line length.display_localsWhen enabled, local variables appearing in the top stack frame code will be displayed with their values.display_trace_localsWhen enabled, local variables appearing in other stack frame code will be displayed with their values.truncate_localsWhen enabled the values of displayed local variables will be truncated to fit the line length.display_arrowWhen enabled an arrow will be displayed for syntax errors, pointing at the offending token.arrow_head_character,arrow_tail_characterCharacters used to draw the arrow which points at syntax errors.inner_exception_messageMessage displayed when one exception occurs inside another, between the two exceptions.  Default isNone, which will simply display the exceptions separated by the header.  If you want to emulate the default non-pretty behaviour, use this:inner_exception_message = pretty_errors.MAGENTA + "\n  During handling of the above exception, another exception occurred:\n"Note that if you usetop_firstthen the order will be reversed, so you should use a message like this instead:inner_exception_message = pretty_errors.MAGENTA + "\n  The above exception occurred during another exception:\n"inner_exception_separatorDefault isFalse.  When set toTruea header will be written before theinner_exception_message.prefixText string which is displayed at the top of the report, just below the header.infixText string which is displayed between each frame of the stack.postfixText string which is displayed at the bottom of the exception report.reset_stdoutWhen 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.header_colorEscape sequence to set header color.timestamp_colorEscape sequence to set timestamp color.exception_colorEscape sequence to set exception color.exception_arg_colorEscape sequence to set exception arguments color.exception_file_colorEscape sequence to set color of filenames in exceptions (for example, in a FileNotFoundError).filename_colorEscape sequence to set filename color.line_number_colorEscape sequence to set line number color.function_colorEscape sequence to set function color.link_colorEscape sequence to set link color.line_colorEscape sequence to set the color of the line of code which caused the exception.code_colorEscape sequence to set the color of other displayed lines of code.arrow_head_color,arrow_tail_colorEscape sequence to set the color of the arrow which points at syntax errors.syntax_error_colorEscape sequence to set the color of the syntax error token.local_name_colorEscape sequence to set the color of local variable names.local_value_colorEscape sequence to set the color of local variable values.local_len_colorEscape sequence to set the color of local value length when local is truncated.pretty_errorshas some built in escape sequence constants you can use when setting these colors:BLACKGREYREDGREENYELLOWBLUEMAGENTACYANWHITEFor each color there is a matchingBRIGHT_variant (i.e.pretty_errors.BRIGHT_RED), as well as a_BACKGROUNDvariant to set the background color (i.e.pretty_errors.RED_BACKGROUND).For example:pretty_errors.configure(line_color=pretty_errors.CYAN_BACKGROUND+pretty_errors.BRIGHT_WHITE)Further customizationFor the most extensive customization (short of forking the package) you may override the defaultExceptionWriterclass, allowing you to tailor the output however you wish.  Typically you will only need to override thewrite_methods.For example:classMyExceptionWriter(pretty_errors.ExceptionWriter):defwrite_header(self):self.output_text('######## ERROR ########')pretty_errors.exception_writer=MyExceptionWriter()Runhelp(pretty_errors.ExceptionWriter)in the python interpreter for more details.