Registry / testing / stackprinter

stackprinter

JSON →
library0.2.13pypypi✓ verified 84d ago

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.

pip install stackprinter
INSTALL
IMPORT
SIG · STACKPRINTER
S
stackprinter
testingpythonv0.2.13
Install
1.5s avg
Import
40ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.2.13 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.040s · 18MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.5s · import 0.039s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

set_excepthook
import stackprinter; stackprinter.set_excepthook()
show
import stackprinter; stackprinter.show()
format
import stackprinter; message = stackprinter.format()
TracePrinter
from stackprinter import TracePrinter

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.

import stackprinter import logging import sys # Option 1: Replace the default excepthook for all uncaught exceptions # stackprinter.set_excepthook(style='darkbg') # Option 2: Use in a try-except block or integrate with logging class VerboseExceptionFormatter(logging.Formatter): def formatException(self, exc_info): # exc_info is a tuple (type, value, traceback) msg = stackprinter.format(exc_info, style='darkbg', source_lines=7, truncate_vals=200) return msg def configure_logger(logger_name=None): formatter = VerboseExceptionFormatter('%(asctime)s %(levelname)s: %(message)s') handler = logging.StreamHandler(sys.stderr) handler.setFormatter(formatter) logger = logging.getLogger(logger_name) logger.addHandler(handler) logger.setLevel(logging.ERROR) return logger logger = configure_logger('my_app') def problematic_function(a, b): result = a / b return result def main(): x = 10 y = 0 try: problematic_function(x, y) except ZeroDivisionError: logger.exception('An error occurred during calculation:') except TypeError as e: # For manual formatting and printing print('\n--- Manual stackprinter output ---\n') stackprinter.show(e, style='darkbg') if __name__ == '__main__': main()
Debug
Known issues
gotchaPython 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.
fix
Upgrade to stackprinter version 0.2.12 or newer (`pip install --upgrade stackprinter`).
affects: <0.2.12
gotchaWhen 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.
fix
Upgrade to stackprinter version 0.2.11 or newer (`pip install --upgrade stackprinter`).
affects: <0.2.11
gotchaIn 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.
fix
Upgrade to stackprinter version 0.2.7 or newer (`pip install --upgrade stackprinter`).
affects: <0.2.7
gotchaAs 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()`.
fix
Call `stackprinter.format(..., suppressed_exceptions=None)` or `stackprinter.show(..., suppressed_exceptions=None)`.
affects: >=0.2.4
gotchaWhen 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.
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.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'stackprinter'
The stackprinter library is not installed in your current Python environment.
fix
pip install stackprinter
TypeError: install() got an unexpected keyword argument 'file'
The `install()` function in stackprinter does not directly accept a `file` argument for output redirection; output configuration is handled via a `formatter` object.
fix
Create a `Formatter` instance with your desired output and pass it to `install`: `import sys; formatter = stackprinter.Formatter(output_dest=sys.stderr); stackprinter.install(formatter=formatter)`
TypeError: default_excepthook() missing 1 required positional argument: 'etype'
The `stackprinter.default_excepthook` function is an internal handler designed to be called by `sys.excepthook` and expects three arguments (`etype`, `evalue`, `etraceback`), so it should not be called manually without them.
fix
Do not call `stackprinter.default_excepthook()` directly. Instead, use `stackprinter.install()` to set it as the global exception hook, or use `stackprinter.format()` to format an exception manually.
AttributeError: module 'stackprinter' has no attribute 'print_exception'
The `stackprinter` module does not have a function named `print_exception`. The correct function to print the current exception information is `print_exc()`.
fix
Replace `stackprinter.print_exception()` with `stackprinter.print_exc()`.
Upgrade
Version history
0.2.13latest on PyPI · released Apr 17, 2026
Audit
Dependencies
pythonrequiredRequires Python 3.4 or higher.
Agent activity
68 hits · last 30 days
node
64
OpenAI (training)
1
Resources