Concepts / Using the Python Interpreter

Using the Python Interpreter

For Windows users, you can run the interpreter in the command line if you have set the PATH variable appropriately.

  • Programming

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.

useshelps findrunsWindows commandlinepython program.pyPATH variableconfigured appropriatelyPython interpreterruns the programprogram.pysaved program
How does Windows use the PATH variable to find and launch the Python interpreter when the user types a command?

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?

Identify the interpreter command: The command begins with python, which is the command used to run the interpreter.

Identify the program file: The saved file is program.py, so that file name follows the interpreter command.

Combine the parts: The complete command is python program.py.

Reuse 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.

Name the function: The function for which information is needed is len.

Use the help functionality: Place the function name inside the help call as help('len').

Read 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.

MethodActionContext
Keyboard shortcutPress Ctrl+DGNU/Linux or OS X shell
CommandEnter exit() followed by EnterGNU/Linux or OS X shell

Ways to exit the interpreter prompt described in the source

Common Usage Mistakes

  • Entering a complete program at the interpreter prompt every time it needs to run.

    The interpreter prompt is not a practical replacement for saving a reusable program file.

    Fix: Save the program in a file and run it with python program.py.

  • Trying to run Python from the Windows command line without an appropriately configured PATH variable.

    The described Windows workflow depends on PATH being set appropriately so the interpreter can be found.

    Fix: Set the PATH variable appropriately before using the interpreter from the Windows command line.

  • Using exit without parentheses.

    The documented command for leaving the interpreter is exit(), including the parentheses.

    Fix: Enter exit() and then press the Enter key.

  • Treating help('len') as a program file command.

    help('len') is used to display information about the len function at the interpreter prompt.

    Fix: Use python program.py to run a saved program, and use help('len') for quick information about len.

Practice the Workflow

EASY

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?

  • help('len')
  • python program.py
  • exit
Reveal answer

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

  1. On Windows, the Python interpreter can be run from the command line when the PATH variable has been set appropriately.
  2. Save programs in files so they can be run any number of times instead of being typed at the interpreter prompt repeatedly.
  3. Run a saved program with python program.py.
  4. Use help('len') to display help for the len function.
  5. In a GNU/Linux or OS X shell, leave the interpreter with Ctrl+D or exit(), followed by Enter for the command form.

Key Takeaways

  • 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.py runs 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.