Python online course

How to Run Python Code: Interactive Shell, IDLE, and Command Line

Learn how to run Python code with the interactive prompt, IDLE, and command-line scripts on Windows, Linux, and macOS.

Ways to Run Python Code

After installing Python, you can execute code interactively or run a saved program. The Python interpreter is the program that reads and executes Python code.

The three common ways to run Python are the interactive prompt, IDLE, and command-line script execution.

MethodWhere code is enteredBest useWhether code is saved automaticallyHow output is shown
Interactive promptIn a terminal sessionQuick calculations, experiments, and syntax testsNoImmediately below the command
IDLEIn an editor window or Shell windowBeginner-friendly editing, running, and basic debuggingOnly when you save the fileIn the IDLE Shell window
Command-line script executionIn a saved .py filePrograms that should be repeated, shared, or run outside an editorYes, in the source fileIn the terminal

A script is a saved Python program, typically stored in a file whose name ends with the .py extension. The .py extension is the conventional way to identify Python source files.

Use the interactive prompt for a short experiment, IDLE for a simple graphical workflow, and command-line execution for repeatable programs.

Run Code in the Interactive Python Prompt

A terminal is a text-based interface for entering operating-system and program commands. On Windows, you can use Command Prompt or PowerShell. Command Prompt is a Windows command-line environment. On Linux and macOS, open Terminal and use a shell, which is a command interpreter commonly used in those systems.

1. Open a terminal

  • Windows: open Command Prompt or PowerShell from the Start menu.
  • Linux: open your terminal application.
  • macOS: open Terminal from Applications or Spotlight.

2. Start the interpreter

Use the command commonly associated with your platform:

PlatformTypical interpreter commandNotes
Windowspy or pythonpy is the Windows Python Launcher when it is installed.
Linuxpython3python3 explicitly requests Python 3 on many systems.
macOSpython3Some systems may also provide a python command.
py
python

On Linux or macOS, use:

python3

If Python starts successfully, the interpreter displays a prompt similar to:

Python 3.x.x ...
>>>

The >>> characters are the interactive prompt. This is an interpreter session that accepts code one command at a time and displays immediate results. It is also called a REPL, meaning Read-Eval-Print Loop: Python reads your input, evaluates it, prints a result when appropriate, and repeats.

3. Enter Python code

At the prompt, enter an expression and press Enter:

>>> 2 + 3
5
>>> print("Hello, Python!")
Hello, Python!

An expression such as 2 + 3 produces a value that the prompt displays. The print() statement writes its message to the session.

Commands entered in the interactive prompt are temporary. They remain available during the current interpreter session, but they are not automatically saved as a program. Save code in a .py file when you want to reuse it.

4. Leave the interpreter

You can enter either of these commands:

exit()
quit()

You can also use a keyboard shortcut:

  • Windows: press Ctrl+Z, then press Enter.
  • Linux and macOS: press Ctrl+D.

Use IDLE

IDLE is Python's lightweight integrated development environment. It commonly comes with standard Python installations and combines a Shell window with an editor.

  • IDLE Shell: an interactive prompt where commands run immediately.
  • Editor window: a workspace for writing and saving a complete Python program.

Launch IDLE by searching for IDLE or Python IDLE in your operating system's application menu. It may also be available through the Python installation's program group.

Create and run a file in IDLE

  1. Open IDLE.
  2. Choose the option to create a new file. This opens an editor window separate from the Shell window.
  3. Enter the following source code:
print("Hello from a script")
  1. Save the editor contents as example.py. Ensure that the filename ends in .py, not .py.txt.
  2. Use IDLE's Run Module or equivalent run command. The usual shortcut is F5.
  3. Look at the IDLE Shell window for the output.
Hello from a script

Errors and tracebacks also appear in the IDLE Shell. IDLE provides basic editing and debugging support, making it suitable for introductory programs. Editing and running are separate actions: opening a file in an editor does not execute it until you use IDLE's run command or a terminal command.

Run a Saved Python Script from the Command Line

Command-line execution is useful when a program should be saved, reused, shared, or run without an editor. Python normally runs a script from top to bottom, and output appears in the terminal.

1. Create and save the script

Create a plain text file named example.py containing:

print("Hello from example.py")

2. Move to the script's folder

The current working directory is the folder from which a command is being run. Use cd to change to the folder containing the file:

cd path-to-folder

Use the normal path syntax for your operating system. Quote a path if a folder name contains spaces, for example:

cd "My Python Projects"

3. Run the file

On Windows, use an available interpreter command:

py example.py
python example.py

On Linux or macOS, commonly use:

python3 example.py

The result is:

Hello from example.py

The interpreter command comes before the script filename. If the file is not in the current working directory, provide a relative path:

python3 projects/example.py

You can also provide a full path:

python3 "/full/path/to/example.py"
"C:\full\path\to\python.exe" "C:\full\path\to\example.py"

Quoted paths are important when a folder or filename contains spaces.

Check Which Python Is Available

To verify that an interpreter is installed and available through the terminal, check its version:

py --version
python --version
python3 --version

Use the command that matches your platform and installation. A successful command reports a Python version, such as Python 3.x.x.

The PATH environment variable is a list of directories that the operating system searches for executable commands. If Python's installation directory is in PATH, you can type python or python3 from many folders without entering the full executable location.

On Windows, the Python executable is commonly named python.exe. The py launcher may be available even when you normally use python. Linux and macOS systems commonly use python3 to distinguish Python 3 from older or unavailable python commands.

Use the executable's complete path

If entering the command alone does not work, temporarily invoke Python by its complete location:

"C:\full\path\to\python.exe" example.py
/full/path/to/python3 example.py

This works even when the installation directory is absent from PATH. A longer-term solution is to add the Python installation directory to PATH. On Windows, the installation's Scripts directory is commonly added as well. Open a new terminal after changing PATH because existing terminal sessions may still have the old environment.

For Windows-specific PATH guidance, see Add Python To The Windows Path.

Troubleshooting

“python” is not recognized or the command is not found

Common causes include Python not being installed, using the wrong command for the platform, or having an installation directory that is not in PATH.

  1. Try the alternatives: py and python on Windows, or python3 on Linux and macOS.
  2. Run the appropriate version command, such as py --version or python3 --version.
  3. If no command finds an interpreter, install Python.
  4. Use the complete executable path as a temporary workaround or configure PATH for future terminal sessions.

Installation references include Install Python On Windows and Install Python On Linux.

The script file cannot be found

This usually means the terminal is in a different folder, the filename or extension is wrong, or the path is malformed.

  • Navigate to the correct folder with cd.
  • Confirm the file is named example.py, rather than example.py.txt.
  • Check spelling and capitalization where the operating system distinguishes them.
  • Use a full path, quoting it when it contains spaces.

IDLE is not visible

Search installed applications for IDLE or Python IDLE. If it is not included, modify or reinstall the Python distribution with IDLE and its standard-library tools. You can also run saved files from a terminal instead.

The wrong Python version starts

Multiple installations may exist, or PATH order may select an unintended interpreter. Check the reported version before running a program. On Windows, use the py launcher with a version selector where applicable, or use the full path to the intended executable. Review PATH after identifying the installation you want to use.

Choosing the Right Method

  • Interactive prompt: best for brief calculations, syntax testing, and learning a small language feature. It provides immediate feedback but does not automatically save your work.
  • IDLE: best for beginners who want a simple graphical environment to write, save, run, and inspect programs. Its editor and basic debugger are convenient for introductory work.
  • Command-line execution: best for saved programs that need to be reused, shared, automated, or run outside an editor.

Once you can run a file, the next useful steps are learning how to write your first program, exploring command-line concepts, and understanding syntax and logical errors.