Skip to main content

Automatically color Python’s uncaught exception tracebacks.

This one’s for anybody who’s ever struggled to read python’s stacktraces on the terminal. Something about the two-lines-per-frame approach really just makes them tough to scan visually.

Compare this:

Traceback (most recent call last):
  File "./workflowy.py", line 525, in <module>
    main()
  File "./workflowy.py", line 37, in main
    projects = cli.load_json(args, input_is_pipe)
  File "./workflowy.py", line 153, in load_json
    return json.load(sys.stdin)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 290, in load
    **kw)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 383, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

To this:

Traceback (most recent call last):
  File "./workflowy.py", line 525, in <module>
    main()
  File "./workflowy.py", line 37, in main
    projects = cli.load_json(args, input_is_pipe)
  File "./workflowy.py", line 153, in load_json
    return json.load(sys.stdin)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 290, in load
    **kw)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 383, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

Installation

Through pip:

pip install colored-traceback

Or directly:

git clone http://www.github.com/staticshock/colored-traceback.py
cd colored-traceback.py
pip install .

On Windows, which has no real support for ANSI escape sequences, this will also install colorama.

Usage

Colored Traceback can be executed as a module:

python -m colored_traceback somefile.py

Colored Traceback also works well within a script or even directly in the interpreter REPL. Standard usage will color the output, unless it’s being redirected to a pipe:

import colored_traceback
colored_traceback.add_hook()

If want to retain color even when stderr is being piped, tack on an always=True argument:

import colored_traceback
colored_traceback.add_hook(always=True)

There are also a couple of convenience imports, which get the footprint down to one line:

# Same as add_hook()
import colored_traceback.auto

# Same as add_hook(always=True)
import colored_traceback.always

It goes without saying that you might want to catch ImportError, making the presence of the package optional:

try:
    import colored_traceback.auto
except ImportError:
    pass

Release files for colored-traceback 0.4.2

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

Source distribution (sdist)

Source distribution for colored-traceback 0.4.2
File Size Uploaded
colored-traceback-0.4.2.tar.gz 4.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for colored-traceback 0.4.2
File Interpreter ABI Platform
colored_traceback-0.4.2-py3-none-any.whl Python 3 none any Details

Total release size: 10.2 kB

Release files / colored-traceback-0.4.2.tar.gz

Download URL colored-traceback-0.4.2.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
ecbc8e41f0712ea81931d7cd436b8beb9f3eff1595d2498f183e0ef69b56fe84
BLAKE2b-256 checksum
How to use checksums
0780afcf567031ab8565f8f8d2bd14b007d313ea3258e50394e85b10a405099c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.4

Release files / colored_traceback-0.4.2-py3-none-any.whl

Download URL colored_traceback-0.4.2-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8ad752a5b3c4e50bccd99d4a2d01e16d80926b519e07a85437ecbf8b4f8ce884
BLAKE2b-256 checksum
How to use checksums
da2805e035e2d95dc469408bcb1edfbee5d4fcfe3b0a5fa3701c5f1f10c77c49
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.4

Release history Release notifications | RSS feed

This release

0.4.2 This release

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

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