The examples that you’ll see in this lesson apply to macOS and Linux. If you’re working on a Windows machine, then it might be handy to have Your Python Coding Environment on Windows: Setup Guide open for reference.
Customizing the Standard REPL
00:00 Customizing the Standard REPL. The Python interpreter lets you customize some of its behaviors and features in an interactive mode. To customize your REPL, you can use a so-called startup file, a Python file that the interpreter reads and executes when you start an interactive session.
00:19 You can also use the Rich third-party library to customize the output of any code that you run in a REPL session. In this section of the course, you’ll learn the basics of how to use these tools to enhance your user experience while working in the Python standard REPL.
00:58 This code will execute before the first prompt is displayed In interactive mode, it’s important to highlight that the startup file runs in the same namespace where you’ll be running your interactive code, so objects defined or imported in this file will be available in your interactive session.
01:14 This behavior is useful when you want to load tools and customize the features of the interactive shell. The first step is to learn how to tell the interpreter which file you want to use as your startup file.
Linux and macOS shells automatically load their corresponding configuration file whenever you fire up a terminal or command-line window. This way, you ensure that the
PYTHONSTARTUP variable is always available on your system.
You can call it whatever you want, and you can put it wherever you want. Just make sure that your
PYTHONSTARTUP environment variable holds the right file path. If you’re on Windows, then check out the Configuring Environment Variables section in Your Python Coding Environment on Windows: Setup Guide.
For a complete guide to creating system variables, follow the instructions and add a
PYTHONSTARTUP system variable with a suitable path. Once you’ve set the
PYTHONSTARTUP variable, go ahead and create the file in the desired folder.
pp() function from the
pprint module allows you to pretty-print formatted data structures, such as lists and dictionaries. To try these new additions, go ahead and open a new terminal or command-line window.
05:51 You’ll find many other interesting tweaks and customizations to add to your REPL’s startup file. So don’t be shy. Go ahead and experiment to improve your user experience and productivity when you work in interactive mode. In the next section of the course, you’ll deepen your understanding of customizing the REPL by looking at colorizing the output using Rich.
Become a Member to join the conversation.