{"id":6893,"library":"stackprinter","title":"Debug-friendly stack traces, with variable values and semantic highlighting","description":"Stackprinter is a Python library that provides enhanced, debug-friendly stack traces. It displays more code context, current variable values, and uses semantic highlighting to make tracebacks more readable. The library is actively maintained, with a current version of 0.2.12, and receives updates periodically to support newer Python versions and address issues.","status":"active","version":"0.2.12","language":"en","source_language":"en","source_url":"https://github.com/cknd/stackprinter","tags":["debugging","traceback","error handling","logging","developer tools"],"install":[{"cmd":"pip install stackprinter","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Requires Python 3.4 or higher.","package":"python","optional":false}],"imports":[{"symbol":"set_excepthook","correct":"import stackprinter; stackprinter.set_excepthook()"},{"symbol":"show","correct":"import stackprinter; stackprinter.show()"},{"symbol":"format","correct":"import stackprinter; message = stackprinter.format()"},{"symbol":"TracePrinter","correct":"from stackprinter import TracePrinter"}],"quickstart":{"code":"import stackprinter\nimport logging\nimport sys\n\n# Option 1: Replace the default excepthook for all uncaught exceptions\n# stackprinter.set_excepthook(style='darkbg')\n\n# Option 2: Use in a try-except block or integrate with logging\n\nclass VerboseExceptionFormatter(logging.Formatter):\n    def formatException(self, exc_info):\n        # exc_info is a tuple (type, value, traceback)\n        msg = stackprinter.format(exc_info, style='darkbg', source_lines=7, truncate_vals=200)\n        return msg\n\ndef configure_logger(logger_name=None):\n    formatter = VerboseExceptionFormatter('%(asctime)s %(levelname)s: %(message)s')\n    handler = logging.StreamHandler(sys.stderr)\n    handler.setFormatter(formatter)\n    logger = logging.getLogger(logger_name)\n    logger.addHandler(handler)\n    logger.setLevel(logging.ERROR)\n    return logger\n\nlogger = configure_logger('my_app')\n\ndef problematic_function(a, b):\n    result = a / b\n    return result\n\ndef main():\n    x = 10\n    y = 0\n    try:\n        problematic_function(x, y)\n    except ZeroDivisionError:\n        logger.exception('An error occurred during calculation:')\n    except TypeError as e:\n        # For manual formatting and printing\n        print('\\n--- Manual stackprinter output ---\\n')\n        stackprinter.show(e, style='darkbg')\n\nif __name__ == '__main__':\n    main()","lang":"python","description":"This quickstart demonstrates two ways to use stackprinter: integrating with Python's standard logging module for automatic verbose exception logging, and manually calling `stackprinter.show()` within an `except` block for immediate, enhanced traceback output. The `set_excepthook()` method (commented out) is also available to globally replace Python's default exception printout."},"warnings":[{"fix":"Upgrade to stackprinter version 0.2.12 or newer (`pip install --upgrade stackprinter`).","message":"Python 3.12 users might encounter issues with multi-line f-strings on stackprinter versions older than 0.2.12, as a fix for this was included in version 0.2.12.","severity":"gotcha","affected_versions":"<0.2.12"},{"fix":"Upgrade to stackprinter version 0.2.11 or newer (`pip install --upgrade stackprinter`).","message":"When handling `ExceptionGroup` objects, older stackprinter versions (prior to 0.2.11) may not fall back gracefully to default tracebacks or might crash. Version 0.2.11 introduced a fix for this.","severity":"gotcha","affected_versions":"<0.2.11"},{"fix":"Upgrade to stackprinter version 0.2.7 or newer (`pip install --upgrade stackprinter`).","message":"In environments where standard output streams (`sys.stdout`, `sys.stderr`) are unavailable (e.g., `pythonw.exe` GUI applications), stackprinter versions older than 0.2.7 might crash when `show()` is called. Version 0.2.7 introduced graceful degradation.","severity":"gotcha","affected_versions":"<0.2.7"},{"fix":"Call `stackprinter.format(..., suppressed_exceptions=None)` or `stackprinter.show(..., suppressed_exceptions=None)`.","message":"As of version 0.2.4, verbose formatting for `KeyboardInterrupt` exceptions is disabled by default. If you require verbose tracebacks for keyboard interrupts, you must explicitly set `suppressed_exceptions=None` when calling `format()` or `show()`.","severity":"gotcha","affected_versions":">=0.2.4"},{"fix":"Be aware of this inherent limitation in concurrent environments. For precise state at the moment of an event, consider using a full interactive debugger.","message":"When inspecting call stacks in multi-threaded programs, variable values displayed by stackprinter represent their state at the time of formatting. These values may have changed in other threads by the time the traceback is printed, potentially leading to misleading information.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}