Recently, there was a tweet by Ned Batchelder that illustrated how to make Python REPL output prettier. I went to implement it, and wanted to put together some instructions for the future.
PYTHONSTARTUP
The PYTHONSTARTUP referenced here is an environment variable that points to a python file that we can place a series of command into that will be evaluated when we launch a Python REPL. I have the following code in a file called .python-startup.py
.
The code above will set the >>>
to a light blue and the ...
to a darker blue, but this isn’t the part you are here for probably. You want the next line, which sets the displayhook for output to pretty print.
Next, you can export the PYTHONSTARTUP environment variable pointing to your file as shown here.
export PYTHONSTARTUP=~/.python-startup.py
You can also add this to your .bashrc
or .zshrc
depending on which shell you are using, and it will always make your python repl output be pretty.
So what is the difference?
First let’s look at the normal output:
Now let’s see it with the pretty print in place:
If your are curious what the colors look like: