Debian Codegraph @

docs/source/traceback.rst

raw
1 Traceback
2 =========
3
4 Rich can render Python tracebacks with syntax highlighting and formatting. Rich tracebacks are easier to read and show more code than standard Python tracebacks.
5
6 To see an example of a Rich traceback, running the following command::
7
8 python -m rich.traceback
9
10
11 Printing tracebacks
12 -------------------
13
14 The :meth:`~rich.console.Console.print_exception` method will print a traceback for the current exception being handled. Here's an example::
15
16 from rich.console import Console
17 console = Console()
18
19 try:
20 do_something()
21 except Exception:
22 console.print_exception(show_locals=True)
23
24 The ``show_locals=True`` parameter causes Rich to display the value of local variables for each frame of the traceback.
25
26 See `exception.py <https://github.com/willmcgugan/rich/blob/master/examples/exception.py>`_ for a larger example.
27
28
29 Traceback Handler
30 -----------------
31
32 Rich can be installed as the default traceback handler so that all uncaught exceptions will be rendered with highlighting. Here's how::
33
34 from rich.traceback import install
35 install(show_locals=True)
36
37 There are a few options to configure the traceback handler, see :func:`~rich.traceback.install` for details.
38
39 Automatic Traceback Handler
40 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
41
42 In some cases you may want to have the traceback handler installed automatically without having to worry about importing the code in your module. You can do that by modifying the `sitecustomize.py` in your virtual environment. Typically it would be located in your virtual environment path, underneath the `site-packages` folder, something like this::
43
44 ./.venv/lib/python3.9/site-packages/sitecustomize.py
45
46 In most cases this file will not exist. If it doesn't exist, you can create it by::
47
48 $ touch .venv/lib/python3.9/site-packages/sitecustomize.py
49
50 Add the following code to the file::
51
52 from rich.traceback import install
53 install(show_locals=True)
54
55 At this point, the traceback will be installed for any code that is run within the virtual environment.
56
57 .. note::
58 If you plan on sharing your code, it is probably best to include the traceback install in your main entry point module.
59
60
61 Suppressing Frames
62 ------------------
63
64 If you are working with a framework (click, django etc), you may only be interested in seeing the code from your own application within the traceback. You can exclude framework code by setting the `suppress` argument on `Traceback`, `install`, `Console.print_exception`, and `RichHandler`, which should be a list of modules or str paths.
65
66 Here's how you would exclude `click <https://click.palletsprojects.com/en/8.0.x/>`_ from Rich exceptions::
67
68 import click
69 from rich.traceback import install
70 install(suppress=[click])
71
72 Suppressed frames will show the line and file only, without any code.
73
74 Max Frames
75 ----------
76
77 A recursion error can generate very large tracebacks that take a while to render and contain a lot of repetitive frames. Rich guards against this with a `max_frames` argument, which defaults to 100. If a traceback contains more than 100 frames then only the first 50, and last 50 will be shown. You can disable this feature by setting `max_frames` to 0.
78
79 Here's an example of printing a recursive error::
80
81 from rich.console import Console
82
83
84 def foo(n):
85 return bar(n)
86
87
88 def bar(n):
89 return foo(n)
90
91
92 console = Console()
93
94 try:
95 foo(1)
96 except Exception:
97 console.print_exception(max_frames=20)
98