Added section `Automatic Traceback Handler` to traceback.rst. It describes a method to automatically install the rich.traceback without having to include the code in your modules.

This commit is contained in:
Troy Williams 2021-10-09 09:37:08 -04:00
parent 18bedc67df
commit 510cec7116
1 changed files with 24 additions and 2 deletions

View File

@ -26,7 +26,7 @@ The ``show_locals=True`` parameter causes Rich to display the value of local var
See `exception.py <https://github.com/willmcgugan/rich/blob/master/examples/exception.py>`_ for a larger example. See `exception.py <https://github.com/willmcgugan/rich/blob/master/examples/exception.py>`_ for a larger example.
Traceback handler Traceback Handler
----------------- -----------------
Rich can be installed as the default traceback handler so that all uncaught exceptions will be rendered with highlighting. Here's how:: Rich can be installed as the default traceback handler so that all uncaught exceptions will be rendered with highlighting. Here's how::
@ -36,6 +36,27 @@ Rich can be installed as the default traceback handler so that all uncaught exce
There are a few options to configure the traceback handler, see :func:`~rich.traceback.install` for details. There are a few options to configure the traceback handler, see :func:`~rich.traceback.install` for details.
Automatic Traceback Handler
^^^^^^^^^^^^^^^^^^^^^^^^^^^
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::
./.venv/lib/python3.9/site-packages/sitecustomize.py
In most cases this file will not exist. If it doesn't exist, you can create it by::
$ touch .venv/lib/python3.9/site-packages/sitecustomize.py
Add the following code to the file::
from rich.traceback import install
install(show_locals=True)
At this point, the traceback will be installed for any code that is run within the virtual environment.
.. note::
If you plan on sharing your code, it is probably best to include the traceback install in your main entry point module.
Suppressing Frames Suppressing Frames
------------------ ------------------
@ -74,3 +95,4 @@ Here's an example of printing an recursive error::
foo(1) foo(1)
except Exception: except Exception:
console.print_exception(max_frames=20) console.print_exception(max_frames=20)