Customize Python Shell

When you load your bash shell, .profile or .bashrc provides initialization script for the shell. It’s possible to achieve the same functionality for the Python shell.

Let’s say, I’d like to modify the Python shell to pretty print all variables and TraceBack(subjective!) using rich library.

  • Create a new file in the home directory.
# Install rich specific things for console pretty printing
from rich.pretty import install
from rich.traceback import install
#from rich.console import Console
#console = Console()
#old_print = print

  • Then while starting the shell pass the file as PYTHONSTARTUP environment variable like PYTHONSTARTUP="/Users/user/" poetry run ipython .

  • Now the output of the variable looks different from the usual ipython output(note the indentation).

  • The same Output without Python shell customization.


  1. Python docs on customization:
  2. Rich library: