Skip to main content

Human readable stacktraces.

Project description

Pretty Traceback

Human readable stacktraces for Python.

Project/Repo:

MIT License Supported Python Versions CalVer 2020.1016 PyPI Version PyPI Downloads

Code Quality/CI:

GitHub Build Status GitLab Build Status Type Checked with mypy Code Coverage Code Style: sjfmt

Overview

Pretty Traceback groups together what belongs together, adds coloring and alignment. All of this makes it easier for you to see patterns and filter out the signal from the noise. This tabular format is best viewed in a wide terminal.

In other words, get this 😍

logo

Instead of this 🤮

Traceback (most recent call last):
  File "test/test_formatting.py", line 199, in <module>
    main()
  File "test/test_formatting.py", line 190, in main
    run_pingpong()
  File "test/test_formatting.py", line 161, in run_pingpong
    sched3.run()
  File "/home/mbarkhau/miniconda3/envs/pretty-traceback_py38/lib/python3.8/sched.py", line 151, in run
    action(*argument, **kwargs)
  File "/home/mbarkhau/miniconda3/envs/pretty-traceback_py38/lib/python3.8/sched.py", line 151, in run
    action(*argument, **kwargs)
  File "/home/mbarkhau/miniconda3/envs/pretty-traceback_py38/lib/python3.8/sched.py", line 151, in run
    action(*argument, **kwargs)
  File "test/test_formatting.py", line 151, in _ping
    _pong(depth + 1)
  File "test/test_formatting.py", line 129, in _pong
    _ping(depth + 1)
  File "test/test_formatting.py", line 136, in _ping
    sp.check_output(["command_that", "doesnt", "exist"])
  File "/home/mbarkhau/miniconda3/envs/pretty-traceback_py38/lib/python3.8/subprocess.py", line 411, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "/home/mbarkhau/miniconda3/envs/pretty-traceback_py38/lib/python3.8/subprocess.py", line 489, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/home/mbarkhau/miniconda3/envs/pretty-traceback_py38/lib/python3.8/subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/home/mbarkhau/miniconda3/envs/pretty-traceback_py38/lib/python3.8/subprocess.py", line 1702, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'command_that'

If your terminal is wide enough, the long paths are preserved.

logo

Usage

Add the following to your __main__.py or the equivalent module which is your entry point.

try:
    import pretty_traceback
    pretty_traceback.install()
except ImportError:
    pass    # no need to fail because of missing dev dependency

Please do not add this code e.g. to your __init__.py or any other module that your users may import. They may not want you to mess with how their tracebacks are printed.

If you do feel the overwhelming desire to import the pretty_traceback in code that others might import, consider using the envvar argument, which will cause the install function to effectively be a noop unless you set ENABLE_PRETTY_TRACEBACK=1.

try:
    import pretty_traceback
    pretty_traceback.install(envvar='ENABLE_PRETTY_TRACEBACK')
except ImportError:
    pass    # no need to fail because of missing dev dependency

Note, that the hook is only installed if the existing hook is the default. Any existing hooks that were installed before the call of pretty_traceback.install will be left in place.

LoggingFormatter

A logging.Formatter subclass is also available (e.g. for integration with flask).

import os
from flask.logging import default_handler

try:
    if os.getenv('FLASK_DEBUG') == "1":
        import pretty_traceback
        default_handler.setFormatter(pretty_traceback.LoggingFormatter())
except ImportError:
    pass    # no need to fail because of missing dev dependency

More Examples

logo

logo

logo

Alternatives

Pretty Traceback is heavily inspired by the backtrace module by nir0s but there are many others (sorted by github stars):

Contributors

Name role since until
Manuel Barkhau (mbarkhau@gmail.com) author/maintainer 2020-08 -

Changelog for https://github.com/mbarkhau/pretty-traceback

2020.1016

  • Shorten tracebacks for RecursionError

2020.1012

  • Improve alias selection

2020.1011

  • Fix github #1: Invalid path handling for ./script.py

2020.1010

  • Fix gitlab #5: Only show aliases that were actually used.
  • Fix gitlab #5: Better alignment on narrow terminals.

2020.1009

  • Fix gitlab #3: Corner case where exception has None as context.
  • Fix gitlab #2: Improve formatting when line overflows.

2020.1008

  • Add pretty_traceback.LoggingFormatter

2020.1006

  • Add wide mode.

2020.1005

  • Update formatting to work better with recursive calls.
  • Add tests

2020.1001

  • Initial release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pretty-traceback-2020.1016.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

pretty_traceback-2020.1016-py2.py3-none-any.whl (12.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pretty-traceback-2020.1016.tar.gz.

File metadata

  • Download URL: pretty-traceback-2020.1016.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.6.0.post20201009 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for pretty-traceback-2020.1016.tar.gz
Algorithm Hash digest
SHA256 4f2bdbab4258320dbaac238c4c4da95009135ee55fba5b2c7c2da7d05fa7c15a
MD5 c837996e199bc32c4f8eb188cd63e638
BLAKE2b-256 a245f86f758037b256733df4091629fbc4d6a58edbc0105a57eccd252f75781e

See more details on using hashes here.

File details

Details for the file pretty_traceback-2020.1016-py2.py3-none-any.whl.

File metadata

  • Download URL: pretty_traceback-2020.1016-py2.py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.6.0.post20201009 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for pretty_traceback-2020.1016-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b6ca1261f7cdd9247106876eb471aafd6004d666a0295ea78f5c155ae6b4d141
MD5 fe27370b173cdaa5ac65541e896fcafe
BLAKE2b-256 568b3bfef7a8bac0bff00da8c52721ae8563756e7fc8a7647bc4c5ed565fa1f4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page