Colorizing REPL Output With Rich
00:00 Colorizing REPL Output With Rich. The Rich library allows you to use rich text and pretty formatting in the terminal. Rich 5.1.0 included highlighted pretty printing.
00:14 You can use this feature to colorize the standard REPLâs output. However, as a third-party library, first you need to install it. Whenever youâre installing a third-party library in Python, itâs good practice to install it into a virtual environment.
00:32 So, on-screen, youâll see one being created and activated for macOS or Linux.
00:47 And here are the commands youâd need for Windows.
01:02 Check out this Real Python course if youâd like to know more about virtual environments. With a virtual environment created and activated, hereâs the command needed to install Rich.
01:21 Once Rich has been installed, you are ready to colorize your REPLâs output. Hereâs how you would do it in an interactive session.
01:44 From this point on, every time you get an output in the REPL session, that output is colorized and formatted. Note that this added capability is temporary.
02:02 It only applies to the current interactive session. Fortunately, though, you know how to use your startup file to add it to every session.
02:29 With this update to the REPLâs startup file, you replicate the output colorizing and formatting behavior in all interactive sessions that have access to the Rich library.
02:39
The try ⦠except ⦠else blocks guarantee that the startup file wonât throw an error if Rich isnât available in the current Python environment that you are using.
02:48 So bear in mind that youâll need to install Rich into any virtual environment that you are using for the colorizing to work in the REPL. In the next section of the course, youâll take a look at some of the features that arenât present in the standard REPL and some alternative REPLs you might want to check out.
Become a Member to join the conversation.

Andras on March 4, 2025
Thanks, I learned something new today.
Your .pythonstartup example made me look into the
try-except-elsestructure, in particular the optionalelseclause. I have never seen this before.As I understand, you want to handle exceptions (suppress them basically with
pass) only from theimportstatement, but let exceptions from eitherinstallpropagate freely up the stack, should there be any. Theelseclause will only run if thetryclause succeeds. As an unexperienced Pythonista I would have added lines 11-12 right after the import line inside thetryclause. But then it is not so crystal clear that we really only want to protect the import statement withtry-except.