Starting and Using the Python Interpreter in a Terminal Window
This page provides operating system-specific advice for starting and the basics of using the Python interpreter in a terminal window, as mentioned in Section 2.2.3 of the textbook. More information on using Python in a terminal window is given in Section 3.2.4 in the textbook.
Starting a Terminal Window and the Command Line Interface
Terminal windows are usually created on a Mac OS X computer by running the Terminal application. On a Linux machine, we can create a terminal window by running xterm, GNOME Terminal, or any of a number of other terminal creating applications. On a Windows computer, we start a Command Prompt. For other operating systems (such as another version of Windows), do a quick Internet search to find out how to start a terminal window.
Section 2.2.3 of the textbook gives an overview of what a command-line interface is, but does not go into detail on how to use such an interface. An Internet search on “using windows command prompt,” “using terminal mac os x,” or “using linux command line” will bring up a list of resources and tutorials on using the command line interface for a Windows, Mac OS X, and Linux computer, respectively.
Starting a Python Interpreter Session in a Terminal Window
Below is a list of ways of getting the interpreter started. The first one listed should work with most if not all users. Try the other steps if the first does not work:
python
(in a Linux or Mac OSX terminal window) or python.exe
(in a Windows command prompt window). If this works it means the computer knows where the installation of Python is located; in technical speak, it means the directory Python is located in is part of the operating system’s path. The path is the list of directories the operating system looks through to find applications. Make sure you are running the Anaconda installation of Python. Many computers come with their own versions of Python that do not include the packages the Anaconda distribution comes with.python
or python.exe
, as applicable. This is done in a terminal or command prompt window, as described in #2 above. The full or absolute path is the path to a location that shows all subdirectories that lead to that location. To find out the full path to the Python interpreter application, we can try the following:
- The Anaconda distribution on Linux or Mac OSX: Check the bin sub-directory. On a Linux machine whose username is myname, the Anaconda distribution’s Python may have this location: /home/myname/anaconda/bin. On a Mac OSX machine, if the Anaconda distribution’s Python has been installed for all users on the machine to access, it may have this location: /Applications/anaconda3/bin.
- The Anaconda distribution on Windows [1]: Type
where anaconda
. The directory anaconda.exe is in is the same one python.exe is in.
python2.7
.When we have successfully started the Python interpreter in a terminal window, we will have something that looks like this:
The first line of the message printed when we start Python tells us the version of Python we are running (above, version 3.6.10, from the Anaconda distribution). The three (>>>
) on the left of the line shows we are now in the Python interpreter. If you are running the default Python for the Anaconda distribution, the (py36)
will not appear on your terminal. That text appears because the terminal that produced it is using conda environments, a more advanced user topic.
Using a Python Interpreter Session in a Terminal Window
Once inside the Python interpreter, we can type in whatever we want to calculate, press the “Enter” key and Python will process what we typed in, as shown in the examples in Chapter 2 of the textbook. Below is a session running the example in Figure 2.1 of the textbook:
When we are done with all the calculations we want to make, to exit the Python interpreter, type quit()
. On Linux and Mac OS X computers, we can also type “Ctrl-d” to quit. On Windows, we can also type “Ctrl-z” and “Return” to quit [2].
To exit from the terminal window, type exit
once we are outside the Python interpreter (at the operating system level). Typing exit
while we are in the Python interpreter will do nothing but tell us what we need to do to leave the Python interpreter.
The “Getting Started With Anaconda” document has more on using Anaconda Prompt or terminal.
Using Help and Info in a Python Interpreter Session in a Terminal Window
If we want to learn more about a function, besides reading the online Python documentation, we can use the built-in function help
or the NumPy function info
for some basic information. For help
, the information is sent a screenful at a time, if we are running the interpreter from the command-line in a terminal window. We type “Space” to page down the help
display, j
to scroll down the display, k
to scroll up the display, and q
to leave the display. For info
, the information scrolls down all at once. We use the window scroll bar to see text that has scrolled off-screen.
Indentation Structures in a Python Interpreter Session in a Terminal Window
In Chapter 3, we learn about defining functions, which uses indented lines of code. See, for instance, Figure 3.2, which we implement in a terminal window below:
For function definitions, the body of the definition is delineated by an indentation of four spaces (which pressing “Tab” will give us when we are in the Python interpreter). The ...
characters seen above are automatically included by the interpreter as we type (if we are running this in a terminal window) as a message to us. They just mean that Python recognizes we are typing in a function definition and is waiting for us to provide the body of the function. They have no other meaning and are not provided in a Jupyter notebook. Besides functions, Python has other structures that use indentation to delineate the body of the structure. Entering those in a terminal window interpreter session will behave similarly to the function example above.
Managing the Current Working Directory on Startup in a Terminal Window
The Python commands described in Section 10.2.1 of the textbook can be used to manage the current working directory when running Python in a terminal window. However, the easiest way to set the current working directory to the directory of our choice is to change to the directory we want to be the current working directory in our Python session and start Python from in there.
Notes
[1] https://stackoverflow.com/a/37117572 (accessed June 23, 2017).
[2] https://stackoverflow.com/a/41524896 (accessed December 24, 2020).