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

Customizing the Standard REPL

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.

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:35 The standard REPL accepts a startup file that you can use to tweak some current features or add new features to your interactive sessions. And this file only runs for interactive sessions.

00:47 It doesn’t run when you execute a program with the python command, so you don’t have to worry about corrupting important programs. The startup file can contain any Python code.

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.

01:28 This is done by setting the PYTHONSTARTUP environment variable in the system Shell. If you’re on Linux or macOS, then you can go to your home folder and open the shell configuration file.

01:42 Once open, add the line seen on-screen to the end of it.

02:10 On macOS, the default shell since Catalina has been Zsh, so the configuration file is a different one.

02:34 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.

02:47 In both these examples, PYTHONSTARTUP is set to .pythonstartup, located in the home folder. Note that the filename isn’t actually important.

02:56 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.

03:14 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.

03:30 Open the file in a text editor and add some code. Let’s start out by adding some imports.

03:39 As you saw earlier in the course, the reload() function allows you to reload modules when you modify their content so you can test your changes.

03:47 Having this function always available in your interactive sessions will be useful. It will prevent repetitive work and save you time.

03:58 The 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.

04:12 You’ll need to open a new one to allow the PYTHONSTARTUP variable to be set correctly. Then run Python in interactive mode. Once there, run the code seen on-screen.

04:26 The pp() function is already available for use. Note that reload() and pp() appear as entries in the current global namespace, which is seen on-screen.

04:36 You can add any imports you need in the REPL’s startup file. This is a nice and quick way to have useful tools at your disposal whenever you run an interactive session.

04:50 Another interesting customization that you make in your REPL’s startup file is to change the characters used to identify the primary (>>>) and secondary (...) prompts.

04:59 To do this, you can change the sys.ps1 and sys.ps2 variables. Go ahead and make the changes seen on-screen to your startup file.

05:19 To try this new look and feel, launch a REPL session. The shell will look similar to what’s seen on-screen.

05:35 Changing the REPL’s prompts is an exciting trick to explore, but in practice, using different prompts may confuse other Python developers who are looking at your screen.

05:44 So you may be better off sticking to the standard prompts.

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.

Avatar image for mp

mp on May 18, 2023

Hi, the link given (to the windows page) doesn’t seem to work. realpython.com/python-coding-setup-windows/

Avatar image for mp

mp on May 18, 2023

ignore the previous comment… local issue!

Become a Member to join the conversation.