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.
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.
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.
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.
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.
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
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: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,
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.
Become a Member to join the conversation.