Concepts / Using the Python Interactive Shell

Using the Python Interactive Shell

Windows then finds and executes the Python interpreter, provided Python is installed and the PATH variable is properly configured.

  • Programming

From Start Menu to Terminal

The Python interactive shell gives you immediate access to a Python prompt where you can execute commands, test code snippets, and interact with Python. On Windows, you begin outside Python by opening the Windows command line terminal.

StartRuncmdCommand promptclicktypepress Enter
What sequence of actions opens a Windows command-line window?
  1. 1
    Click the Start button.
  2. 2
    Click Run.
  3. 3
    Type cmd in the Run dialog.
  4. 4
    Press Enter.
  5. 5
    Wait for the Windows command prompt window to open.

Launching Python from Windows

When the command prompt window opens, it provides a text-based interface for interacting with the Windows operating system. At this point, you are in the Windows command line environment, not yet in Python. Type python at the command prompt and press Enter. Windows then finds and executes the Python interpreter, provided Python is installed and the PATH variable is properly configured.

UserWindows terminalPython interpreter>>>type python and press Enterlaunchesdisplays
What command is entered, and what happens when Windows starts Python?
Worked example

Launching Python

You have opened the Windows command prompt and want to enter the Python interactive shell.

  1. 1
    Enter the launch command

    At the Windows command prompt, type python and press Enter.

  2. 2
    Wait for interpreter startup

    Windows launches the Python interpreter.

  3. 3
    Inspect the result

    Python displays version information and then shows the >>> prompt.

Result

The >>> prompt indicates that Python is ready to accept Python commands.

Recognizing the Python Prompt

Successful startup is visible in the terminal. Python displays version information, followed by three greater-than symbols: >>>. The exact version number can differ according to the Python version installed, but the version information and the >>> prompt are the important signs to look for.

Windows commandpromptC:\Users\YourName>Python prompt>>>Version informationpython and Enterready for Python code
How does the terminal change when Python starts successfully, and which prompt confirms that it is ready?
EnvironmentVisible promptWhat it expects
Windows command lineSomething like C:\Users\YourName>Windows commands
Python interactive shell>>>Python code

The prompt identifies which environment is currently active.

Moving Between Environments

Running Python from the Windows command line creates a hierarchy of environments. The Windows operating system is the bottom layer. The command-line terminal provides a text-based way to communicate with that operating system. When you type python, the terminal launches the Python interpreter. The >>> prompt shows that you are now working inside that interpreter.

Commands belong to the environment whose prompt is visible. A Windows command-line prompt expects Windows commands. The Python prompt expects Python code. A command that works in one environment may not work in the other, so check the prompt before entering an instruction.

  1. 1
    Begin at the Windows command prompt.
  2. 2
    Type python and press Enter.
  3. 3
    Confirm that version information appears.
  4. 4
    Confirm that the prompt changes to >>>.
  5. 5
    Enter Python commands only while >>> is visible.
  6. 6
    To return to Windows, type exit() at the Python prompt and press Enter.

Mistakes with the Two Prompts

  • Common mistake

    Assuming the Windows command prompt is already the Python shell.

    What it looks like

    The terminal window is open, but the >>> prompt has not appeared.

    Why it fails

    Opening the terminal only places you in the Windows command-line environment. Python has not started yet.

    Do this instead

    Type python at the Windows command prompt and press Enter, then look for version information and >>>.

  • Common mistake

    Treating >>> as a Windows command prompt.

    What it looks like

    Trying to enter Windows commands after the >>> prompt appears.

    Why it fails

    The >>> prompt belongs to the Python interpreter and expects Python code.

    Do this instead

    Use Python code while >>> is visible. Type exit() and press Enter when you need to return to the Windows command line.

  • Common mistake

    Failing to verify startup.

    What it looks like

    Assuming Python is ready without checking the displayed prompt.

    Why it fails

    The visible version information and >>> prompt are the signs that the interpreter has started and is waiting for input.

    Do this instead

    Check for both version information and the >>> prompt before entering Python commands.

Guided Terminal Check

EASY

Practice the complete transition on a Windows system: open the Run dialog from Start, enter cmd, press Enter, type python at the Windows command prompt, and press Enter. Before you do anything else, identify which prompt is visible. Then state whether you are in the Windows command line or the Python interactive shell, and explain what the visible prompt tells you.

Hints
  • The Windows command line prompt looks something like C:\Users\YourName>.
  • The Python interactive shell displays version information and ends with >>>.
  • The >>> prompt means that Python is ready to accept Python code.

Shell Startup Checklist

  1. 1
    Open the Windows command line through Start, Run, cmd, and Enter.
  2. 2
    At the Windows command prompt, type python and press Enter.
  3. 3
    Verify startup by looking for Python version information and the >>> prompt.
  4. 4
    Remember that the Windows command prompt expects Windows commands, while >>> expects Python code.
  5. 5
    Use exit() at the Python prompt to return to the Windows command line.

Key Takeaways

  • The Windows Run dialog provides a quick way to open the command-line terminal.
  • Typing python at the Windows command prompt launches the Python interpreter when Python is installed and PATH is properly configured.
  • Version information followed by >>> confirms that Python is running and ready for input.
  • The Windows command prompt and the Python prompt are different environments with different expected commands.
  • Typing exit() at >>> returns you to the Windows command line.

Related concepts

More concepts starting with U