Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

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 tryexceptelse 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.

Avatar image for Andras

Andras on March 4, 2025

Thanks, I learned something new today.

Your .pythonstartup example made me look into the try-except-else structure, in particular the optional else clause. I have never seen this before.

As I understand, you want to handle exceptions (suppress them basically with pass) only from the import statement, but let exceptions from either install propagate freely up the stack, should there be any. The else clause will only run if the try clause succeeds. As an unexperienced Pythonista I would have added lines 11-12 right after the import line inside the try clause. But then it is not so crystal clear that we really only want to protect the import statement with try-except.

Become a Member to join the conversation.