Sub topic 5
PATH and the python, python3, and py commands
This chapter assumes you can open a terminal and have installed Python 3 using one of the methods from the previous chapter. You should know what a command and a folder path are. Here you will learn how the terminal finds Python, and why the right command name differs between systems.
How the terminal finds a program
When you type a command such as python3, the shell does not search your whole disk. It searches a list of folders called PATH. PATH is an environment variable, a named setting that the operating system passes to every program. The shell checks each folder in order and runs the first match it finds.
On macOS and Linux, folders in PATH are separated by colons. On Windows, they are separated by semicolons.
echo $PATH
# Windows Command Prompt: echo %PATH%
# Windows PowerShell: $env:PathIf Python's folder is not in PATH, the shell reports "command not found" (macOS and Linux) or "is not recognized as an internal or external command" (Windows). Python is installed in that case, but the shell cannot see it.
The three command names
Different systems expose Python under different names.
| Command | Where it is common | Notes |
|---|---|---|
| `python3` | macOS, Linux | Always means Python 3 when present. |
| `python` | Windows, virtual environments, some Linux setups | May be missing, or on older systems may point to Python 2. |
| `py` | Windows only | The Python launcher, installed by the python.org installer. |
The py launcher is a small program that finds the Python versions installed on Windows and starts one. It does not depend on Python's folder being in PATH, which makes it the most reliable choice on Windows. You can ask for a specific version with a flag.
py --version
py -3.12 --version
py -0py -0 lists every Python version the launcher can find. The -3.12 form starts that exact minor version, if it is installed.
Finding which Python you are running
Several Python installations can exist on one computer. To see which one a command resolves to, ask the shell for the full path.
which python3
# Windows Command Prompt: where python
# Windows PowerShell: Get-Command pythonPython can also report its own location. This is useful because it shows the interpreter that is actually running, not the one you expected.
import sys
print(sys.executable)
print(sys.version)Save this as where.py and run it. If the path points somewhere unexpected, such as a system folder when you wanted a virtual environment, you have found the cause of many setup problems.
Adding Python to PATH
The Windows installer has a checkbox labeled "Add python.exe to PATH". Select it during installation. If you missed it, you can rerun the installer and choose Modify, or use the py launcher instead. After changing PATH, close and reopen the terminal. Terminals read PATH once at startup, so an open window will not see the change.
On macOS and Linux, you add a folder by editing your shell's startup file, such as ~/.zshrc or ~/.bashrc:
export PATH="$HOME/tools/python/bin:$PATH"Placing the new folder before $PATH makes the shell find it first. Placing it after means an older match elsewhere wins.
Common errors
- Typing
pythonon a system that only haspython3. Usepython3. - Typing
python3on Windows. Usepyorpython. - Changing
PATHand not restarting the terminal. - Two Pythons installed, with the older one earlier in
PATH.
Exercise
You run python3 --version and see Python 3.8, but you installed 3.12 an hour ago. Name two checks that would help you find the cause.
Show answer
Run which python3 (or where python on Windows) to see which installation the shell resolves first. Print sys.executable from inside Python to confirm the running interpreter. Likely causes are an older folder earlier in PATH, or a terminal opened before the install that has not reloaded PATH.