Skip to main content

Python stack formatter

This prints detailed Python stack traces, with more source context and current variable values. It's a quick way to see what your code is doing when your only debugging tool is a log file 😱

Before

After

Installation

pip install stackprinter

Logging exception tracebacks

Call show or format inside an except block to trace the current exception. show prints to stderr, format returns a string. You can also pass exception objects explicitly.

By default, this will generate plain text. Pass style='color' to get funky terminal colors. For all the config options, see the docs of format().

import stackprinter

try:
    something()
except:
    stackprinter.show()  # grab the current exception and print a traceback to stderr

    # ...or only return a string, e.g. for logging.
    message = stackprinter.format()
    logging.log(message)

There's also stackprinter.set_excepthook which replaces the default python crash message (so it works automatically without manual try/except... unless you're in IPython).

Printing the call stack of another thread

Pass a thread object to show or format.

thread = threading.Thread(target=something)
thread.start()
while True:
    stackprinter.show(thread) # or format(thread)
    time.sleep(0.1)

Printing the call stack of the current thread

Call show or format outside of exception handling.

stackprinter.show() # or format()

For people who don't like context dependent methods, there's also show/format_current_stack(), which does the same even inside except blocks.

Tracing a piece of code as it is executed

More for curiosity than anything else, you can watch a piece of code execute step-by-step, printing a trace of all calls & returns 'live' as they are happening. Slows everything down though, of course.

tp = stackprinter.TracePrinter(style='color', suppressed_paths=[r"lib/python.*/site-packages/numpy"])
tp.enable()
a = np.ones(111)
dosomething(a)
tp.disable()

How it works

Basically, this is a frame formatter. For each frame on the call stack, it grabs the source code to find out which source lines reference which variables. Then it displays code and variables in the neighbourhood of the last executed line.

Since this already requires a map of where each variable occurs in the code, it was hard not to also implement the whole semantic highlighting color thing seen in the screenshots. The colors are ANSI escape codes now, but it should be fairly straightforward™ to render the underlying data without any 1980ies terminal technology. Say, a foldable and clickable HTML page with downloadable pickled variables. But for now you'll have to pipe the ANSI strings through ansi2html or something.

Caveats

This displays variable values as they are at the time of formatting. In multi-threaded programs, variables can change while we're busy walking the stack & printing them. So, if nothing seems to make sense, consider that your exception and the traceback messages are from slightly different times. Sadly, there is no responsible way to freeze all other threads as soon as we want to inspect some thread's call stack (...or is there?)

Docs

*coughs*

For now, just look at all the doc strings, e.g. of format()

Metadata

Release files for stackprinter 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stackprinter 0.1
File Size Uploaded
stackprinter-0.1.tar.gz 19.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for stackprinter 0.1
File Interpreter ABI Platform
stackprinter-0.1-py3-none-any.whl Python 3 none any Details

Total release size: 42.5 kB

Release files / stackprinter-0.1.tar.gz

Download URL stackprinter-0.1.tar.gz
Size 19.7 kB
Tags Source
SHA-256 checksum
How to use checksums
6bd23ed44a27c9a2141d9c41bb7ec94b357d3f4be311de45997ca2bb05954d72
BLAKE2b-256 checksum
How to use checksums
d932ec857f11f069a456b3ec133179e6359ed594313366b59f43aa56694bb375
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

Release files / stackprinter-0.1-py3-none-any.whl

Download URL stackprinter-0.1-py3-none-any.whl
Size 22.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
71192949afd6e9f7bf806f1e037706a95de8cb51483d4be93c7077cf7de2d27a
BLAKE2b-256 checksum
How to use checksums
7ab38124101b1359e3fb852194260c8dedf08958cd88370e8ec9b296a9901e14
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

Release history Release notifications | RSS feed

0.2.13

2 release files

0.2.12

2 release files

0.2.11

2 release files

0.2.10

2 release files

0.2.9

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

This release

0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page