Skip to main content
https://www.codeshelter.co/static/badges/badge-flat.svg https://img.shields.io/badge/code%20style-black-000000.svg

TBVaccine is a utility that pretty-prints Python tracebacks. It automatically highlights lines you care about and deemphasizes lines you don’t, and colorizes the various elements in a traceback to make it easier to parse.

Here are some screenshots. This is the before:

misc/before.png

And this is the after:

misc/after.png

If you add the hook or call TBVaccine in your code, it can also print all variables in each stack frame. That is, it turns this:

misc/before-vars.png

into this:

misc/after-vars.png

Installation

To install, use pip:

pip install tbvaccine

You are done!

Global usage

You can have TBVaccine insert itself all up in your system and stick its tentacles in all your libraries, like a cute, useful Cthulhu. That way, every single Python traceback in your system will be pretty. Just set the TBVACCINE environment variable to 1, and you’re done.

E.g. for bash:

export TBVACCINE=1

Or fish:

set -x TBVACCINE=1

Usage as a command-line utility

TBVaccine can be used from the command line several ways.:

python -m tbvaccine myscript.py

Or just pipe STDERR into it from the program you want to watch:

./myscript.py 2>&1 | tbvaccine

And all the tracebacks will now be pretty!

Usage as a Python library

There are various ways to use TBVaccine as a Python library.

Initialize it like so:

from tbvaccine import TBVaccine
tbv = TBVaccine(
    code_dir="/my/code/dir",
    isolate=True
)

code_dir marks the directory we code about. Files under that directory that appear in the traceback will be highlighted. If not passed, the current directory, as returned by os.getcwd() will be used.

If isolate is False, all lines are colorized, and code_dir is ignored.

If show_vars is False, variables will not be printed in each stack frame.

To use it in an except block:

from tbvaccine import TBVaccine
try:
    some_stuff()
except:
    print(TBVaccine().format_exc())

To make it the default way of printing tracebacks, use add_hook() (which also accepts any argument the TBVaccine class does):

import tbvaccine
tbvaccine.add_hook(isolate=False)

1 / 0

Bam! Instant pretty tracebacks.

Logging integration

You can integrate TBVaccine with logging like so:

class TbVaccineFormatter(logging.Formatter):
    def  formatException(self, exc_info):
        return TBVaccine(isolate=True).format_exc()

sh = logging.StreamHandler()
sh.setFormatter(TbVaccineFormatter('[%(levelname)s] %(asctime)s : %(message)s', '%Y-%m-%d %H:%M:%S'))
logger.addHandler(sh)

Epilogue

This library is still pretty new, please contribute patches if something doesn’t work as intended, and also please tell your friends about it! Hopefully one day it will be implemented in the Python interpreters themselves.

– Stavros

Release files for tbvaccine 0.3.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 tbvaccine 0.3.1
File Size Uploaded
tbvaccine-0.3.1.tar.gz 7.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tbvaccine 0.3.1
File Interpreter ABI Platform
tbvaccine-0.3.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 15.6 kB

Release files / tbvaccine-0.3.1.tar.gz

Download URL tbvaccine-0.3.1.tar.gz
Size 7.4 kB
Tags Source
SHA-256 checksum
How to use checksums
8665d037ac8ad4c97bc44512de91798565b1226f7b2f59669bb7dbe9a1f87718
BLAKE2b-256 checksum
How to use checksums
b22ee8a5f61ae44deb508778fc828fe9cc167d5bbc5e594f016f5c477aabf293
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.6

Release files / tbvaccine-0.3.1-py2.py3-none-any.whl

Download URL tbvaccine-0.3.1-py2.py3-none-any.whl
Size 8.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
3aae41b75e237f2840dde274722b1070ca43d65dd24cf60344bd6d345c77cc3d
BLAKE2b-256 checksum
How to use checksums
b1fb84c930393fb23cf5fef93629df6370c51639306f0f403b9628f1d44b3b26
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.6

Release history Release notifications | RSS feed

This release

0.3.1 This release

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.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.3

1 release file

0.0.2

2 release files

0.0.1

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