Sub topic 5
PATH and the python, python3, and py commands
This chapter assumes you can open a terminal, run a command, and have installed Python 3 using one of the methods from the previous chapter. You do not need to know any Python yet. The goal is a working model of how a terminal turns the word python into a running interpreter, and of what to check when it does not.
What PATH is
When you type a command such as python3, the shell does not search your whole disk. It looks through a list of directories called PATH, in order, and runs the first executable file with a matching name. PATH is an environment variable, which is a named setting that the operating system passes to every program it starts.
On macOS and Linux the directories are separated by colons. On Windows they are separated by semicolons.
echo $PATH
# Example output on Linux or macOS:
# /usr/local/bin:/usr/bin:/bin:/home/ada/.local/bin# Windows Command Prompt
echo %PATH%
# Windows PowerShell
$env:Path -split ';'Two consequences follow from this model:
- If the directory that holds the interpreter is not in
PATH, the shell reports "command not found" even though Python is installed. - If two directories both hold a file called
python3, the one listed first wins. This is how you end up running a different Python from the one you just installed.
Three names for the interpreter
The same interpreter can be reached under different names depending on the platform and how it was installed.
| Command | Where you usually see it | Notes |
|---|---|---|
| `python3` | macOS, Linux | The standard name for Python 3 on Unix-like systems. |
| `python` | Windows, virtual environments, some Linux distributions | May be missing, or on older systems may point to Python 2. |
| `py` | Windows only | The Python launcher, installed by the python.org installer. |
On macOS and most Linux distributions, python3 is the safe choice. Many distributions do not provide python at all unless you install an extra package, because the name historically meant Python 2.
On Windows, py is the most reliable choice. It does not depend on PATH containing the Python directory, because the launcher installs into a location that Windows already searches. It finds Python installations through the registry that the installer writes.
Inside an activated virtual environment, python always refers to that environment's interpreter, on every platform. You will use that in a later chapter.
Asking which Python you are running
When a command behaves unexpectedly, ask the system what it is actually running before changing anything.
# macOS and Linux
which python3
type -a python3
# Windows Command Prompt
where python
where py# Windows PowerShell
Get-Command python | Format-List Source
Get-Command py | Format-List Sourcewhich prints the first match. type -a prints every match in PATH order, which shows shadowing. where on Windows lists all matches.
You can also ask the interpreter to describe itself. This works the same on every platform once you have a command that starts Python.
import sys
print(sys.executable)
print(sys.version)
print(sys.prefix)Save this as whichpython.py and run it. sys.executable is the full path of the interpreter that is running the file. This is the most direct answer to "which Python is this?".
The Windows launcher in detail
The py launcher selects an interpreter for you. Without arguments it picks the newest Python 3 it finds. You can request a specific version.
py --version
py -3 --version
py -3.12 --version
py -0py -0 (or py --list) lists every installed interpreter the launcher knows about. This is useful when you have several versions installed and want to confirm which one is the default.
The launcher also makes a common Windows problem easier to avoid. If you skipped the "Add python.exe to PATH" checkbox during installation, python will fail but py will still work. You can keep using py for everything in this tutorial.
Adding Python to PATH
The best time to do this is during installation. The python.org Windows installer has a checkbox labeled "Add python.exe to PATH" on its first screen. Selecting it adds the install directory and its Scripts directory to your user PATH.
If you missed it, you can edit PATH afterwards.
On Windows:
- Open the Start menu and search for "Edit environment variables for your account".
- Select
Pathunder "User variables" and choose Edit. - Add the directory that contains
python.exe, for exampleC:\Users\Ada\AppData\Local\Programs\Python\Python312\. - Add the
Scriptssubdirectory next to it, which is where tools such aspipplace their commands. - Open a new terminal. Existing terminals keep the old value.
On macOS and Linux, a change goes in your shell startup file. For bash that is ~/.bashrc or ~/.bash_profile. For zsh, the macOS default, it is ~/.zshrc.
# Add a directory to the front of PATH for future shells
echo 'export PATH="/opt/python3.12/bin:$PATH"' >> ~/.zshrc
# Apply it to the current shell without opening a new one
source ~/.zshrcPutting the new directory at the front makes it win over any other python3. Putting it at the end makes it a fallback. Choose deliberately.
Common failure patterns
Most setup problems reduce to a small set of patterns. The first step in each case is to run which, where, or the sys.executable script and compare the result with what you expected.
| Symptom | Likely cause | Fix |
|---|---|---|
| `python3: command not found` | Not installed, or its directory is not in `PATH` | Install Python, or add its `bin` directory to `PATH`. |
| `'python' is not recognized` on Windows | The PATH checkbox was skipped | Use `py`, or edit `PATH` and open a new terminal. |
| `python` opens the Microsoft Store | App execution alias intercepts the name | Use `py`, or disable the alias. |
| `python --version` shows 2.7 | An older Python is earlier in `PATH` | Use `python3`, or reorder `PATH`. |
| `python --version` shows the wrong 3.x | Several versions installed, an older one first | Check with `type -a` or `py -0`, then reorder or request a version. |
| Version is right but `pip` installs to a different Python | `pip` and `python` come from different directories | Run `python -m pip` so both come from the same interpreter. |
| Fix applied but nothing changed | The terminal still has the old `PATH` | Open a new terminal, or run `source` on the startup file. |
The python -m pip form in the sixth row is worth adopting as a habit. The -m option tells the interpreter to run a module as a program. Since the interpreter you named is the one that runs pip, the packages go to the right place.
A short diagnostic routine
When a Python command fails or behaves oddly, work through these steps in order.
- Run the command with
--versionto see whether it exists and which version it reports. - Find where it lives with
which,type -a,where, orGet-Command. - Compare the location with where you installed Python.
- If it is the wrong one, either use a different command name (
python3,py -3.12) or fixPATHorder. - Open a fresh terminal after any
PATHchange, then repeat step 1.
import os
import sys
print("Interpreter:", sys.executable)
for index, folder in enumerate(os.environ["PATH"].split(os.pathsep), start=1):
print(index, folder)This script prints the interpreter path and then each PATH directory in search order. It uses os.pathsep, which is : on Unix-like systems and ; on Windows, so the same file works everywhere. Reading the numbered list shows which directory the shell would try first.
Exercise
On a Linux machine, python3 --version prints "Python 3.8.10", but you just installed Python 3.12 into /opt/python3.12/bin. Give the commands you would use to confirm why the old version runs, and the line you would add to ~/.bashrc so that 3.12 runs when you type python3.
Show answer
Run type -a python3 to list every python3 in PATH order. It will show the system copy (for example /usr/bin/python3) ahead of, or instead of, /opt/python3.12/bin/python3. Then add export PATH="/opt/python3.12/bin:$PATH" to ~/.bashrc, so the new directory comes first. Run source ~/.bashrc or open a new terminal and check again with python3 --version.
Exercise
On Windows you installed Python but python --version opens the Microsoft Store. Name two ways to get a working command without reinstalling.
Show answer
Use the launcher with py --version, which does not rely on PATH. Or add the Python install directory and its Scripts directory to your user PATH and open a new terminal. Turning off the Python app execution aliases in Windows settings also stops the Store from intercepting the name.
Exercise
Why is python -m pip install requests safer than pip install requests when you have more than one Python installed?
Show answer
The pip command is found by searching PATH and may belong to a different Python than the python command. Running python -m pip starts pip as a module of the exact interpreter you named, so the package is installed for that interpreter.