VMware ESXi and vSphere Cluster Management
How to Run Python Code: Interactive Prompt, IDLE, and Command Line
Learn how to run Python code with the interactive prompt, IDLE, and saved .py files from Windows Command Prompt or a Linux shell.
After installing Python, you can run code in several beginner-friendly ways. The three most common are the interactive Python prompt, IDLE, and command-line execution of a saved script.
| Method | Where It Runs | Best Use | Typical Action | Key Limitation or Requirement |
|---|---|---|---|---|
| Interactive prompt | A terminal session | Quick experiments and testing one statement or expression | Start Python, then enter code at the prompt | Code is not automatically saved as a program |
| IDLE | A graphical Python environment | Editing, running, and basic debugging for beginner programs | Open the editor, save a module, and run it from the GUI | IDLE must be included with the Python installation |
| Command-line script execution | Windows Command Prompt or a Linux shell | Running saved programs and repeatable commands | Pass a .py filename to the Python interpreter | The interpreter and script path must be available |
How Python code is executed
The Python interpreter is the program that reads and executes Python code. You can give it code interactively, open code through IDLE, or ask it to execute a saved file.
A script is a saved file containing Python instructions. Python source files normally use the .py extension, such as example.py.
The command line is a text-based terminal interface. On Windows, this is commonly Command Prompt. On Linux, it is commonly a shell such as Bash. The command used to start Python can vary by operating system and installation.
Run code in the interactive Python prompt
The interactive prompt is a command-line Python session. It is also called a REPL, which means Read-Eval-Print Loop: Python reads what you enter, evaluates it, prints a result when appropriate, and waits for more input.
1. Open a terminal
- Windows: Open Command Prompt from the Start menu.
- Linux: Open your terminal application.
2. Start the interpreter
Try the command appropriate for your installation:
python
On many Linux systems, Python 3 is started explicitly:
python3
On Windows installations that provide the Python launcher, you can use:
py
A successful launch displays information about Python followed by a prompt that commonly looks like >>>.
3. Enter an expression
At the prompt, enter an expression and press Enter:
>>> 2 + 2
4
You can also enter a statement such as:
>>> print("Hello, Python!")
Hello, Python!
The result appears immediately, which makes the REPL useful for trying syntax, checking calculations, and exploring Python features.
4. Leave the interactive session
Use one of these options:
- Enter
exit()and press Enter. - Enter
quit()and press Enter. - On Windows, press
Ctrl+Z, then Enter. - On Linux, press
Ctrl+D.
After leaving Python, you return to the normal operating-system command prompt or shell.
Use IDLE to edit and run Python code
IDLE is Python's basic integrated development environment. It includes a Python shell for immediate commands and an editor for source files. IDLE is typically installed with Python, although some installation choices or distributions may omit it.
Open IDLE
- Windows: Search the Start menu for IDLE, such as “IDLE (Python 3.x).”
- Linux: Open it from the application menu if it was installed, or start it from a terminal with an available IDLE command.
The IDLE Shell window works like the interactive prompt. Enter a command after >>> to see its result immediately.
Create and run a file in IDLE
- Open IDLE.
- Choose the option to create a new file in the editor.
- Enter a short program:
print("Hello, Python!")
- Save the file with a
.pyextension, for exampleexample.py. - Run the saved module using IDLE's Run command, commonly available from the Run menu.
- Read the output in the IDLE Shell window.
IDLE provides basic editing features, syntax highlighting, and beginner-level debugging support. It is useful when you want a graphical workflow without installing a larger development environment.
Run a saved Python file from the command line
When Python is started by itself, it opens an interactive session. When a filename follows the interpreter command, Python executes that file instead.
First create a file named example.py containing:
print("Hello, Python!")
Then open Command Prompt or a Linux shell and run the file:
python example.py
Depending on your system, the equivalent commands may be:
python3 example.py
py example.py
The command has two parts: the interpreter command starts Python, and example.py tells it which saved program to execute.
Run a script from its folder
The terminal has a working directory, meaning the current folder in which commands are being run. If the script is in that folder, its filename is enough:
python example.py
If the script is elsewhere, change to its folder first:
cd path-to-script-folder
python example.py
You can also provide a relative or absolute path directly:
python path-to-script-folder/example.py
On Windows, a path may use a drive letter and backslashes:
python C:\path\to\example.py
Python commands and the PATH environment variable
The PATH environment variable is a system setting containing directories that a terminal searches when you enter a command. If the directory containing the Python interpreter is in PATH, commands such as python can be found from any working directory.
If the terminal says that Python is “not recognized,” “not found,” or cannot be located, common causes include:
- Python is not installed.
- The Python installation directory is not in PATH.
- Your system uses a different command, such as
python3orpy. - Several Python installations exist and the command points to another one.
Try the available launch commands
| Platform | Possible Interpreter Command | Notes |
|---|---|---|
| Windows | python or py | The Python launcher command py may be available even when command behavior differs. |
| Linux | python3 or sometimes python | Many Linux systems use python3 to identify Python 3 explicitly. |
| Systems where Python 3 is invoked explicitly | python3 | Use this when python is unavailable or refers to another version. |
Check which command works and verify its version:
python --version
python3 --version
py --version
Run only the command that is available on your system. The output should identify the installed Python version.
Use the full interpreter path
One solution is to provide the complete filesystem path to the interpreter. This does not depend on PATH:
"C:\path\to\python.exe" example.py
On Linux, the equivalent form is:
/full/path/to/python3 example.py
This is useful for testing a specific installation or working around an incorrectly configured PATH.
Add Python to PATH
Another solution is to add the directory containing the Python executable to the system PATH. After changing PATH, open a new terminal and run a version check such as:
python --version
Adding Python to PATH makes the interpreter easier to launch, but multiple installations can make command selection confusing. Verify the version before running an important program.
Troubleshoot common problems
Python cannot be found
Try python3 on Linux or py on Windows. If neither works, check whether Python is installed, use its full path, or add its installation directory to PATH.
The script cannot be opened
An error such as “no such file” usually means the terminal is in the wrong working directory, the filename is incorrect, or the path was omitted. Use cd, check the exact spelling and .py extension, or provide the complete path.
cd path-to-script-folder
python example.py
The wrong Python version starts
Multiple Python installations may exist, or PATH may list one installation before another. Check the reported version and use python3, py, a version-specific launcher option if available, or an explicit interpreter path.
IDLE is unavailable
Search the installed applications for IDLE. If it is missing, modify or reinstall Python with the IDLE component included. Until then, you can use the interactive prompt or command-line script execution.
Which method should you choose?
- Choose the interactive prompt for a quick calculation or a small experiment.
- Choose IDLE when you want a simple graphical editor, shell, and basic debugging tools.
- Choose command-line script execution when your code is saved and you want to run it repeatedly or as part of a terminal workflow.
For more practice, return to running Python code and try each method with the same example.py program.