In this lesson, you’ll learn about the various ways to start and end a REPL interactive session. You’ll try out a sampling of command-line options, but you may want to learn more. In that case, you can turn to the course Command Line Interfaces in Python for a deeper dive.
Starting and Ending REPL Interactive Sessions
00:00 Starting and Ending REPL Interactive Sessions. The Python standard REPL is available in every Python installation. To start a new session, you just need to run the Python interpreter in an interactive mode from your command line.
00:16 This mode will put you into a shell environment where you can execute Python code. Learning how the input code works is a way of getting immediate feedback on it.
Once you’ve installed Python on your computer, then you can immediately start benefiting from this tool. To start a Python interactive shell or session, open a command-line window and run the
python command without arguments.
00:41 This command makes Python enter its interactive mode. The interpreter will run, and you’ll get an output similar to what’s seen on-screen. The first line of this output shows information about your current Python version and the platform on which you are running it.
The second line shows a message with commands that you can run to get additional information about Python in general. The last line shows the primary prompt of a standard Python interactive session or shell. By default, this prompt consists of three greater-than signs (
>>>), also known as chevrons.
Its purpose is to communicate that the interpreter is ready to accept input. The interpreter also has a secondary prompt represented by three dots (
01:28 This prompt appears when you’re entering compound statements or line continuations. You’ll learn more about this later on in the course. Now that you are in the Python REPL, the next question may well be how you get out.
01:43 If you’re used to working on your command line or terminal, then you probably don’t like closing and opening terminal windows all the time. Your regular workflow may go through executing CLI tools, closing them when the work is done, and then returning to your current shell session. Once inside the Python REPL, you can’t run normal shell commands because you are inside a different environment. To get back to the normal shell, you need to terminate the REPL session.
There are a few different ways to do this. You can either use
exit(). These two functions are built into Python. Therefore, they’re available to you at any moment in an interactive session.
Both functions allow you to exit the current session by implicitly raising a
SystemExit exception. You’ll see other ways of exiting the REPL later on.
python command can take a number of command-line options. A few of them can be useful when working in a REPL session. The
-c command-line option allows you to quickly run a Python statement or expression that you provide as a string on your command line.
03:00 Try out the command that’s seen on-screen.
One of the most relevant options in this context is the
-i flag. This option makes the interpreter enter the interactive mode. After running a script or executing a piece of code using the
-c option, you can use the
-i option to check the current global variables in your script or to inspect the stack trace when your program raises an exception.
03:33 To try this out, let’s say you have the script that’s seen on-screen.
03:45 This script reads some sample data for a file or database and provides a function to compute the mean, or average, of the data.
Run the script with the command seen on-screen. Once you press Enter, this command runs the code in
sample.py and takes you directly to an interactive session.
You’ll recognize this session because your screen will present the REPL’s primary prompt (
>>>). From this point on, you can inspect, test, and debug the code in
sample.py as needed. First, you call the built-in
globals() function to inspect the global names defined in the script.
This function returns a dictionary that maps names to their corresponding objects. Next, you call
mean() with a new sample of data. Note that when you run a piece of code in an interactive session, you typically get immediate feedback as an output on your screen.
You’ll recognize the code’s output because it won’t have a leading prompt. Finally, you call
mean() with an empty list as an argument. In this case, the function fails with a
ZeroDivisionError because calling
len() with an empty list returns
Note that when you use the
-i command-line option with the
python command, the
PYTHONSTARTUP environment variable won’t be read.
You’ll learn more about this environment variable later on in the course. The
-b flag is another command-line option to consider when you run Python in an interactive mode.
This option comes in handy when you’re running code that compares
bytes objects, and you want to get a warning if a string or integer value gets in the middle of a comparison.
-b option seen here makes the interpreter display a warning when it finds operations that compare bytes with either strings or
06:01 If you don’t use this option, then no warning is seen.
As seen previously, the comparison returns
False because the values are of different data types. However, here you don’t get any warning that helps you understand why you are getting this result.
06:28 This is just a sampling of the options that may come in handy when you’re using Python in interactive mode. For a complete list of command-line options, check out this Real Python course.
Earlier you saw that you can use
quit() to leave a REPL session, but there are other methods available to you. Some you may use every day.
06:49 Others you may only see or use occasionally. A quick option for terminating a REPL session is to use a keyboard shortcut, which will depend on the operating system you are running on: Control + D on Unix systems, such as Linux or macOS,
07:08 or Control + Z and then Enter on Windows systems.
These key combinations represent the end-of-file character in the corresponding operating system. They allow you to exit the current interactive session because the interpreter runs in a special file called
__main__, as you can confirm by inspecting the
All the Python code in an interactive session will be contained in the
__main__ file, which runs until an end-of-file character is read. This means that when the interpreter finds this character, it immediately terminates the current REPL session.
Alternatively, you can explicitly raise the
SystemExit exception manually with an exit code of
0. You’ll get the same result, and your current REPL session will terminate.
08:04 Any of these methods will get you out of your current Python interactive session and take you back to the operating system shell. After this, you can run regular commands once more.
08:19 In the next section of the course, you’ll look at how to run code within a REPL session.
Become a Member to join the conversation.