2020-02-22 20:04:20 +00:00
Traceback
=========
Rich can render Python tracebacks with syntax highlighting and formatting. Rich tracebacks are easier to read, and show more code, than standard Python tracebacks.
Printing tracebacks
-------------------
2020-02-22 20:14:23 +00:00
The :meth: `~rich.console.Console.print_exception` method will print a traceback for the current exception being handled. Here's an example::
2020-02-22 20:04:20 +00:00
2021-08-03 15:51:39 +00:00
import rich
2020-02-22 20:04:20 +00:00
try:
do_something()
except:
2021-08-03 15:51:39 +00:00
console = rich.console.Console()
console.print_exception(show_locals=True)
2020-02-22 20:04:20 +00:00
2021-08-03 15:51:39 +00:00
The `` show_locals=True `` parameter causes Rich to display the value of local variables for each section of the traceback.
2020-02-22 20:04:20 +00:00
Traceback handler
-----------------
Rich can be installed as the default traceback handler so that all uncaught exceptions will be rendered with highlighting. Here's how::
from rich.traceback import install
2021-08-03 15:51:39 +00:00
install(show_locals=True)
2020-03-08 18:34:56 +00:00
2021-08-03 15:51:39 +00:00
There are a few options to configure the traceback handler, see :func: `~rich.traceback.install` for details.