Colored Traceback
Colored-traceback is a Python library that automatically colors uncaught exception tracebacks, aiming to improve readability in the terminal. The project is currently active, with its latest release (0.4.2) on July 13, 2024, but has an infrequent release cadence.
Common errors
-
Tracebacks appear with unexpected or double coloring on Python 3.13+.
cause Conflict with Python 3.13's built-in colored traceback feature.fixRemove `colored-traceback` from your environment if using Python 3.13 or newer, or investigate methods to disable Python's native colorization if you prefer `colored-traceback`'s output. -
Tracebacks are monochrome (not colored) when running my script and piping output to another tool or saving to a file.
cause By default, `colored-traceback` disables color output when `stderr` is not detected as an interactive terminal (TTY).fixModify your activation call to `colored_traceback.add_hook(always=True)` or use the convenience import `import colored_traceback.always`. -
AttributeError: module 'colored_traceback' has no attribute 'auto' (or 'always' or 'add_hook')
cause Incorrect import statement or a damaged/incomplete installation.fixEnsure you are importing correctly as `import colored_traceback.auto`, `import colored_traceback.always`, or `import colored_traceback` followed by `colored_traceback.add_hook()`. If the issue persists, try reinstalling the package: `pip uninstall colored-traceback && pip install colored-traceback`.
Warnings
- breaking Python 3.13 and newer versions introduce native colorized tracebacks. Using `colored-traceback` with Python 3.13+ may lead to unexpected behavior, double coloring, or conflicts with the built-in system.
- gotcha Tracebacks may not appear colored when the output (stderr) is redirected or piped to another command/file.
- gotcha On Windows, `colored-traceback` automatically installs the `colorama` package to provide ANSI escape sequence support, which might be an unexpected dependency for some users.
Install
-
pip install colored-traceback
Imports
- add_hook
from colored_traceback import add_hook # Direct import of add_hook is not the intended primary usage for activation.
import colored_traceback colored_traceback.add_hook()
- auto
import colored_traceback.auto
- always
import colored_traceback.always
Quickstart
import colored_traceback.auto
def cause_error():
return 1 / 0
def main():
print("Attempting to cause an error...")
cause_error()
if __name__ == "__main__":
main()