Skip to main content

Human readable stacktraces.

Project description

Pretty Traceback

Human readable stacktraces for Python.

Project/Repo:

MIT License Supported Python Versions PyCalVer 2020.1012 PyPI Version PyPI Downloads

Code Quality/CI:

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

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

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 "/home/user/venvs/py38/bin/myproject", line 12, in <module>
    sys.exit(cli())
  File "/home/user/venvs/py38/lib/python3.8/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/home/user/venvs/py38/lib/python3.8/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/home/user/venvs/py38/lib/python3.8/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/user/venvs/py38/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/user/venvs/py38/lib/python3.8/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/home/user/foss/myproject/src/myproject/cli.py", line 148, in build
    lp_gen_docs.gen_html(built_ctx, html_dir)
  File "/home/user/foss/myproject/src/myproject/gen_docs.py", line 295, in gen_html
    wrapped_html = wrap_content_html(content_html, 'screen', meta, toc)
  File "/home/user/foss/myproject/src/myproject/gen_docs.py", line 238, in wrap_content_html
    result = tmpl.render(**ctx)
  File "/home/user/venvs/py38/lib/python3.8/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/home/user/venvs/py38/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/home/user/venvs/py38/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 56, in top-level template code
TypeError: no loader for this environment specified

If you're terminal is not wide enough, the long paths are replaced with aliases.

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.

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

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.1012.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

pretty_traceback-2020.1012-py2.py3-none-any.whl (11.8 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: pretty-traceback-2020.1012.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.2 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for pretty-traceback-2020.1012.tar.gz
Algorithm Hash digest
SHA256 f93a80df8fcf0ecec44847dd15efecf6aafd1c4c83f18f521b4f5ef65deee08f
MD5 21473d94c3f55f4f8348f63f0deb82bd
BLAKE2b-256 7ce02dc6a39b5cecc8a72e4421f068d759219ba214b0e9af7373ec555a31dd40

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pretty_traceback-2020.1012-py2.py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.2 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for pretty_traceback-2020.1012-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 52fd1c6c1b76b2fbc1c36b3dfd2f35bff530a51660437515ffaad054b1bb765d
MD5 5f4ea611eb68727be246cbe325489891
BLAKE2b-256 570ba087e4b2db9eb1811832a159f16b7617644cb5d10318cd3239eb9094ecde

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