Run Python Scripts from the Command Line
Learn how to save Python code as a .py file, navigate to its folder, and run it from Windows, Linux, or macOS command-line shells.
What the command line is
The command line is a text-based interface used to run programs and work with files. You type a command, press Enter, and the computer performs the requested action.
A shell is the command interpreter that processes those typed commands. On Windows, common shells include Command Prompt, the traditional Windows command-line shell, and PowerShell, a more feature-rich Windows shell that can also run Python commands. On Linux and macOS, the Terminal application provides access to shells such as Bash or Zsh.
A terminal or shell is different from an editor or IDE such as IDLE. An editor is where you write and save source code. The shell is where you start the Python interpreter and run the saved file. You can use an IDE to create a script and a terminal to execute it.
When you enter Python statements in an interactive interpreter, Python runs each statement as you enter it. A saved script is a file containing Python statements. When you run the script, the Python interpreter reads and executes the file from beginning to end.
Save Python code as a script file
Create a plain-text file in a text editor or IDE and save your Python code in it. The conventional filename extension for Python source files is .py. A filename such as example.py tells you, and commonly your tools, that the file contains Python source code.
For this lesson, create a file named example.py with the following contents:
x = 5
y = 10
z = x + y
print(z)x = 5 and y = 10 assign numeric values to variables. The expression x + y adds those values, and the assignment stores the result in z. The print(z) statement displays the result in the terminal.
The expected output is:
15On Windows, be careful that the file is not accidentally saved as example.py.txt. Some editors and file managers hide known extensions. Enable visible filename extensions, or use the file manager's rename function to confirm that the true filename ends in .py.
Open a command-line shell
Windows
Open Command Prompt from the Start menu, or open PowerShell. Both accept commands such as cd and can start Python. Their prompts may look different, but the basic Python commands are similar.
Linux and macOS
Open the Terminal application. The terminal provides access to a shell, which accepts commands such as cd, ls, and python3.
Every shell has a current working directory. This is the folder the shell currently treats as the default location for file commands. A script's short filename works directly only when the script is in that directory.
Navigate to the script directory
Use cd to change the current working directory to the folder containing example.py. Then list the folder contents to confirm that the file is present.
Windows Command Prompt or PowerShell:
cd C:\Users\YourName\Documents\Python
dir
Linux or macOS:
cd ~/Documents/Python
lsdir lists files in Windows Command Prompt. ls lists files in Linux and macOS shells and is also available as a common alias in PowerShell.
A path containing spaces must be enclosed in quotation marks. Without quotes, the shell may treat each word as a separate argument:
cd "C:\Users\YourName\My Python Files"
cd "/Users/yourname/My Python Files"A relative path describes a location relative to the current working directory. The name example.py is a relative path when the file is in the current folder.
Run a script with the Python interpreter
The Python interpreter is the program that executes Python source code. The basic command pattern is:
interpreter-command script-filenameThe script filename is a command-line argument: a value supplied after a command. The interpreter reads and executes the named .py file.
When Python is available through PATH and the shell is already in the script's folder, use:
python example.pyOn many Linux and macOS systems, Python 3 is started with python3:
python3 example.pyOn Windows installations that include the Python launcher, use:
py example.pyUse the command appropriate for your operating system and installation. A successful run prints 15 and then normally returns to the shell prompt.
Commands by platform
| Task | Windows Command Prompt or PowerShell | Linux/macOS shell |
|---|---|---|
| Check Python version | python --version or py --version | python3 --version or python --version |
| Display Python help | python -h, py -h, or python /? | python3 -h |
| Change to the script folder | cd C:\path\to\folder | cd /path/to/folder |
| List files | dir or ls in PowerShell | ls |
| Run a script | python example.py or py example.py | python3 example.py |
| Find the interpreter command | where python or where py | which python3 or which python |
Run a script using an absolute path
An absolute path is a complete file location beginning at a drive root on Windows or the filesystem root on Linux and macOS. Use an absolute script path when you do not want to change directories, or when the script is not in the current working directory.
Windows:
python "C:\Users\YourName\Documents\Python\example.py"
Linux or macOS:
python3 "/home/yourname/Documents/Python/example.py"Quote the script path when it contains spaces. The interpreter path and the script path are separate: the first identifies the Python executable, while the second identifies the source file to run.
If the interpreter command is not available through PATH, provide the full path to the Python executable as well:
Windows:
"C:\Path\To\Python\python.exe" "C:\Users\YourName\Documents\Python\example.py"
Linux or macOS:
/path/to/python3 "/home/yourname/Documents/Python/example.py"Understand PATH and find Python
PATH is an environment variable containing a list of directories. When you type a program name such as python, the shell searches those directories for a matching executable.
If the shell says that python is not recognized, cannot be found, or is not a command, Python may not be installed, its installation directory may not be on PATH, or your system may use another command such as python3 or py.
Check which interpreter commands are available:
Windows:
where python
where py
Linux or macOS:
which python
which python3If no suitable interpreter is found, install Python. On Windows, select the installer option to add Python to PATH when available. You can also use the interpreter's full executable path instead. After changing PATH, open a new terminal window before testing again.
For Windows-specific PATH guidance, see Add Python to the Windows Path. Installation details are available in Install Python on Windows and Install Python on Linux.
Check Python versions and command options
Python accepts options, also called switches, that change what the interpreter does. Use a version option to identify the installation selected by a command:
python --version
python3 --version
py --versionVersion output helps you confirm which Python installation is being used, especially when multiple installations exist.
Use -h to display interpreter help and available options:
python -h
python3 -h
py -hWindows command conventions also commonly support:
python /?The -h form is broadly supported by Python. The slash-style form is associated with Windows command conventions.
Read output and errors
Normal output produced by print() appears in the terminal. For example.py, successful execution displays 15, then returns to the prompt.
If the script fails, Python usually prints a traceback. A traceback is Python's diagnostic report showing where an exception occurred. It normally includes the filename, a line number, and an error type or message.
For a syntax or indentation problem, read the filename and line number, correct the saved code, save the file, and run the same command again. The terminal executes the version currently saved on disk, not unsaved changes still open in an editor.
Ways to run a Python script
| Method | Example pattern | When to use it |
|---|---|---|
| Script name from its own folder | python example.py | The shell's current directory contains the script and python is available. |
| Absolute script path | python "C:\Folder With Spaces\example.py" | You want to run the script without changing folders. |
Windows py launcher | py example.py | The Windows Python launcher is installed or python is not the preferred command. |
| Explicit interpreter and explicit script path | "C:\Path\To\Python\python.exe" "C:\Folder\example.py" | Neither the interpreter nor the script can be reliably found through the current directory or PATH. |
Troubleshooting common errors
| Symptom | Likely cause | Resolution |
|---|---|---|
| Python command is not recognized | Python is not installed, is not on PATH, or another launcher command is required. | Try python3 --version or py --version; use where or which; then use a full interpreter path or install Python and add it to PATH. |
| Cannot open the script file | The current directory or supplied path is wrong, the filename is misspelled, or a spaced path was not quoted. | Run dir or ls, use cd, check the exact filename, and try a quoted absolute path. |
| File appears to be missing | Windows may have hidden the extension, so the file is actually example.py.txt. | Enable filename extensions, rename the file to end in .py, and confirm with dir. |
| Syntax error or traceback appears | The saved code has invalid syntax, indentation problems, or a runtime error; alternatively, the latest edits were not saved. | Read the traceback's filename and line number, correct the code, save it, and run the command again. |
| Unexpected Python version runs | Multiple installations exist, and PATH selects a different interpreter first. | Check version output, use the Windows launcher where appropriate, or run the desired interpreter by its full path. |
Complete workflow
- Create a plain-text file named
example.py. - Enter the addition program and save the file.
- Open Command Prompt, PowerShell, or a Linux/macOS terminal.
- Use
cdto move to the folder containing the file, or prepare its absolute path. - Use
dirorlsto confirm that the file exists. - Check the interpreter with
python --version,python3 --version, orpy --version. - Run
python example.py,python3 example.py, orpy example.py, depending on your system. - Confirm that the terminal displays
15and returns to the prompt.
For related fundamentals, review What Are Variables, Arithmetic Operators, Write Your First Program, and Interactive Prompt.