Pretty Traceback
Human readable stacktraces for Python.
Project/Repo:
Code Quality/CI:
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 😍
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.
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
Alternatives
Pretty Traceback is heavily inspired by the backtrace module by nir0s but there are many others (sorted by github stars):
- https://github.com/qix-/better-exceptions
- https://github.com/cknd/stackprinter
- https://github.com/onelivesleft/PrettyErrors
- https://github.com/skorokithakis/tbvaccine
- https://github.com/aroberge/friendly-traceback
- https://github.com/HallerPatrick/frosch
- https://github.com/nir0s/backtrace
- https://github.com/staticshock/colored-traceback.py
- https://github.com/chillaranand/ptb
- https://github.com/laurb9/rich-traceback
- https://github.com/willmcgugan/rich#tracebacks
Contributors
| Name | role | since | until |
|---|---|---|---|
| Manuel Barkhau (mbarkhau@gmail.com) | author/maintainer | 2020-08 | - |
Changelog for https://github.com/mbarkhau/pretty-traceback
2024.1021
- Update: Avoid leading / on relative paths. gh#13
2023.1020
- Fix: Prevent errors in non-tty environments gh#9
2023.1019
- Update: Append lineno to filename so it can be parsed by editors/IDEs. gh#8
2022.1018
- Add final newline to output. See gh#3
2021.1017
- Fix highlight in wide mode
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
Noneas 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
Metadata
Release files for pretty-traceback 2024.1021
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pretty-traceback-2024.1021.tar.gz | 18.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pretty_traceback-2024.1021-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 30.4 kB
Release files / pretty-traceback-2024.1021.tar.gz
| Download URL | pretty-traceback-2024.1021.tar.gz |
|---|---|
| Size | 18.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
08c53c7737a315e5b17646340f1620c1b9c7ef3cbbc195ac05e50d0b131101f7
|
|
BLAKE2b-256 checksum How to use checksums |
fe1a5238fa3aa2310d47af50069f451a314f75d3998cb99c07189bfe1ae6c54b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.10.12
|
Release files / pretty_traceback-2024.1021-py2.py3-none-any.whl
| Download URL | pretty_traceback-2024.1021-py2.py3-none-any.whl |
|---|---|
| Size | 12.3 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
c1f351b9e3fce79fb4343a6509a5f2bad16084356c63fe85d3f6bbb0486dd94a
|
|
BLAKE2b-256 checksum How to use checksums |
f309336a8a63f6e85ff57d1ab87c6aa0179e2e68b34c9e113d7064e63fbbb99c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.10.12
|