Sunday, February 23, 2020

Will McGugan: Better Python tracebacks with Rich

One of my goals in writing Rich was to render really nice Python tracebacks. And now that feature has landed.

I've never found Python tracebacks to be a great debugging aid beyond telling me what the exception was, and where it occurred. In a recent update to Rich, I've tried to refresh the humble traceback to give enough context to diagnose errors before switching back to the editor.

Here's an example of a rich traceback:

© 2020 Will McGugan

Rich traceback on OSX

There is syntax highlighting to help pick out filename, line, and function, etc. There's also a snippet of code for each stack frame, with line numbers and syntax highlighting. It's configurable, but I find that 7 lines of code are enough to make it relatable to the file in your editor, and give you a better understanding of the context that lead to the exception.

Here's how tracebacks render on Windows:

© 2020 Will McGugan

Rich traceback on Windows terminal

For reference, here's the same traceback rendered in a more traditional way:

© 2020 Will McGugan

Just a regular old traceback

To try out rich tracebacks, install the exception handler as follows:

from rich.traceback import install
install()

Now any uncaught exceptions will rendered by Rich. See the docs for details.

Rich is quite usable as a library now, but is still in active development. If you have any ideas on how to improve rich tracebacks or any other aspect of the library, let me know.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...