Using the Python Interpreter
The interpreter prompt is suited to quick interaction. A saved program file is suited to running the same program repeatedly.
From Prompt to Program
The Python interpreter is useful when you want to work at the interpreter prompt, but entering a complete program there every time would be impractical. Python programs are therefore saved in files so that they can be run any number of times. Using the interpreter involves choosing between immediate interaction at the prompt and running a saved program from the command line.
The interpreter prompt is suited to quick interaction. A saved program file is suited to running the same program repeatedly.
Finding Python on Windows
For Windows users, the Python interpreter can be run from the command line when the PATH variable has been set appropriately. The PATH variable is the part of this process that allows the command-line use of the interpreter. When you enter the command to run a program, Windows uses the configured PATH information to find and launch Python.
Running a Saved File
Once a program has been saved in a file, run the interpreter with the command python program.py. In this command, python runs the interpreter and program.py identifies the saved program to run. The important change is that the program is no longer being typed out at the interpreter prompt each time; it is being read from a saved file.
Running the Same Program Again
A Python program has been saved in a file named program.py. How can it be run from the command line?
- 1Identify the interpreter command
The command begins with python, which is the command used to run the interpreter.
- 2Identify the program file
The saved file is
program.py, so that file name follows the interpreter command. - 3Combine the parts
The complete command is python
program.py. - 4Reuse the file
Because the program is saved in a file, it can be run any number of times rather than being typed at the interpreter prompt each time.
Run the program with python program.py.
Saving a program separates writing it from running it. The saved file can be passed to the interpreter whenever the program needs to run.
Getting Help at the Prompt
The interpreter prompt is also a convenient place to request quick information about a Python function or statement. Python provides built-in help functionality for this purpose. For example, running help('len') displays help for the len function, which is used to count the number of items.
Requesting Help for len
You need quick information about the len function while using the interpreter prompt.
- 1Name the function
The function for which information is needed is len.
- 2Use the help functionality
Place the function name inside the help call as
help('len'). - 3Read the displayed information
The interpreter displays help for len, the function used to count the number of items.
Run help('len') when you need quick information about len.
Leaving the Interpreter
In a GNU/Linux or OS X shell, you can leave the interpreter prompt in either of two ways: press Ctrl+D, or enter exit() followed by the Enter key. The exit() form requires the parentheses.
| Method | Action | Context |
|---|---|---|
| Keyboard shortcut | Press Ctrl+D | GNU/Linux or OS X shell |
| Command | Enter exit() followed by Enter | GNU/Linux or OS X shell |
Ways to exit the interpreter prompt described in the source
Common Usage Mistakes
Common mistake Entering a complete program at the interpreter prompt every time it needs to run.
What it looks likeThe same program is repeatedly typed into the prompt instead of being saved.
Why it failsThe interpreter prompt is not a practical replacement for saving a reusable program file.
Do this insteadSave the program in a file and run it with python program.py.
Common mistake Trying to run Python from the Windows command line without an appropriately configured PATH variable.
What it looks likeThe command-line workflow is attempted before PATH has been set appropriately.
Why it failsThe described Windows workflow depends on PATH being set appropriately so the interpreter can be found.
Do this insteadSet the PATH variable appropriately before using the interpreter from the Windows command line.
Common mistake Using exit without parentheses.
What it looks likeEntering exit instead of exit().Why it failsThe documented command for leaving the interpreter is
exit(), including the parentheses.Do this insteadEnter exit() and then press the Enter key.
Common mistake Treating help('len') as a program file command.
What it looks likeUsing the help request as though it were the command for running a saved program.
Why it failshelp('len')is used to display information about the len function at the interpreter prompt.Do this insteadUse python program.py to run a saved program, and use help('len') for quick information about len.
Practice the Workflow
A Windows user has a saved file named program.py and has set the PATH variable appropriately. Decide which action matches each goal: run the saved program, request information about len, or leave the interpreter prompt in a GNU/Linux or OS X shell.
Hints
- The saved-program command is python
program.py. - The help request is
help('len'). - The documented exit choices for a GNU/Linux or OS X shell are Ctrl+D and
exit().
What do you think happens?
You want to run a saved file named program.py from the command line. Which command should you choose?
Reveal answer
python program.py
The source gives python program.py as the command for running a saved Python program. help('len') requests information about len, while exit() is used to leave the interpreter prompt in a GNU/Linux or OS X shell.
Practical Takeaways
- 1On Windows, the Python interpreter can be run from the command line when the PATH variable has been set appropriately.
- 2Save programs in files so they can be run any number of times instead of being typed at the interpreter prompt repeatedly.
- 3Run a saved program with python
program.py. - 4Use
help('len')to display help for the len function. - 5In a GNU/Linux or OS X shell, leave the interpreter with Ctrl+D or
exit(), followed by Enter for the command form.
At a glance
- The interpreter can be used interactively, but reusable programs should be saved in files.
- On Windows, an appropriately configured PATH variable supports running Python from the command line.
- The command python
program.pyruns a saved program. - The built-in help functionality can provide quick information, such as
help('len'). - In a GNU/Linux or OS X shell, Ctrl+D or
exit()can leave the interpreter prompt.