Sub topic 6
Checking that Python is installed
This chapter assumes you can open a terminal and run a command, and that you have followed an installation method from the previous chapters. By the end you will be able to confirm that Python works, find out which Python a command runs, and diagnose the usual failures.
What "installed" means
A successful install means three things are true:
- The interpreter, the program that runs Python code, exists on disk.
- Your terminal can find it by name, because its folder is on the
PATH(the list of folders the shell searches for commands). - The version is one you intended to install.
A check that only proves the first point can mislead you. An interpreter can exist and still be unreachable from your terminal. Your terminal can also find a different Python from the one you just installed. The checks below test all three points.
Step 1: Ask for the version
Run the version command for your system.
python3 --version
# On Windows, use: py --version
# If your system maps python to Python 3, this also works: python --versionA working install prints one line, such as Python 3.12.4. The first number must be 3. The second number is the minor release, and it tells you which language features you have.
| System | Command to try first | Fallback |
|---|---|---|
| Windows | `py --version` | `python --version` |
| macOS | `python3 --version` | `python --version` |
| Linux | `python3 --version` | `python --version` |
Which name works depends on how Python was installed. On Windows, the py launcher is installed by the python.org installer and chooses among the installed versions. On macOS and Linux, python3 is the reliable name. The bare python command may be missing, or on an old system it may point to Python 2.
Step 2: Find out which interpreter ran
A version number does not say where the interpreter lives. When you have several Pythons (a system one, a Homebrew one, a python.org one), you need the path.
python3 -c "import sys; print(sys.executable); print(sys.version_info[:3])"The -c option runs a string of Python code and exits. sys.executable is the full path of the interpreter that is running, and sys.version_info[:3] is the major, minor, and micro numbers as a tuple, for example (3, 12, 4).
Your shell can also tell you what a name resolves to, without starting Python.
command -v python3
which -a python3command -v shows the first match, which is the one that runs. which -a lists every match on PATH, in search order. On Windows Command Prompt, use where python instead. In PowerShell, use Get-Command python. To list every Python the py launcher knows about, with paths, run py -0p.
The order of the list matters. The shell runs the first match and ignores the rest. If an older Python appears above the new one, that is the one you get.
Step 3: Check pip belongs to the same Python
pip is the package installer. A common hidden fault is a pip command that belongs to a different Python than the one you run. Avoid the problem by invoking pip through the interpreter.
python3 -m pip --versionThe output ends with the Python it is tied to, for example pip 24.0 from /usr/lib/python3/dist-packages/pip (python 3.12). The -m option runs a module as a program. Writing python3 -m pip guarantees that pip installs into the interpreter you just checked. Plain pip offers no such guarantee.
On some Linux distributions, pip is a separate package. If this command says No module named pip, install your distribution's pip package, such as python3-pip on Debian and Ubuntu.
Step 4: Run a check script
For a repeatable check, put the logic in a file. This script fails with a clear message if the interpreter is too old. Save it as check_python.py.
import sys
MINIMUM = (3, 9)
if sys.version_info < MINIMUM:
raise SystemExit(
f"Python {MINIMUM[0]}.{MINIMUM[1]}+ required, "
f"found {sys.version.split()[0]}"
)
print(f"OK: Python {sys.version.split()[0]} at {sys.executable}")Run it with python3 check_python.py (or py check_python.py on Windows). Comparing sys.version_info to a tuple works because tuples compare element by element. Raising SystemExit with a string prints the message to standard error and exits with status 1, so shell scripts and CI jobs can detect the failure. Change MINIMUM to whatever your project needs.
A decision path for failures
If a check does not print a Python 3 version, work through the diagram in order.
Common failures in detail
| Symptom | Cause | Fix |
|---|---|---|
| `python3: command not found` (Linux, macOS) | Python 3 is not installed, or its folder is not on `PATH` | Install it with your package manager or the python.org installer, then open a new terminal |
| `'python' is not recognized` (Windows) | The installer's "Add Python to PATH" option was off | Use `py`, or rerun the installer and enable the option |
| Microsoft Store opens, or a message says Python was not found | Windows ships placeholder `python.exe` aliases that point to the Store | Install Python, or turn off the aliases in Settings under "Manage app execution aliases" |
| `Python 2.7.x` printed | The bare `python` name points to a legacy interpreter | Run `python3`, or fix the order of `PATH` |
| Old version prints after you installed a new one | An older Python comes first on `PATH` | Run `which -a python3` (or `where python`) and reorder or remove entries |
| macOS shows a dialog about developer tools | The `python3` stub from Apple needs the Command Line Tools | Accept the install, or install Python from python.org or Homebrew instead |
Edge cases worth knowing
A terminal reads PATH once, when it starts. If you installed Python while a terminal was open, that terminal does not see the change. Close it and open a new one before you conclude that the install failed.
An active virtual environment changes the answer. Inside an activated environment, python points to the environment's interpreter. A check run there tells you about the environment, not about the system. A prompt prefix such as (.venv) is the signal. Run deactivate to check the system Python.
Several versions can coexist. You may have python3.11 and python3.12 side by side. python3 is only one name for one of them. Versioned names such as python3.12 --version remove the ambiguity. On Windows, py -3.12 --version selects a version.
Exit status is a check you can script. The command succeeds (status 0) only if the interpreter starts. In Bash, python3 --version && echo ok prints ok only when the check passes. In a CI job this is a cheap first step.
What a complete check looks like
A finished verification, on Linux or macOS, looks like this:
python3 --version
command -v python3
python3 -m pip --version
python3 check_python.pyYou should see a 3.x version, a path you recognize, a pip line that names the same Python version, and the OK: line from the script. If those four outputs agree with each other, the interpreter, PATH, and pip are consistent, and you can move on to the interactive interpreter.
Exercise
You installed Python 3.12 and then ran python3 --version in a terminal that was already open. It printed Python 3.9.6. What are two likely explanations, and which commands would you run to tell them apart?
Show answer
Either the open terminal has an old PATH and resolves to a previously installed Python, or an older Python appears earlier on PATH than the new one. Open a new terminal and rerun the check to rule out the stale PATH. If the old version still appears, run which -a python3 (or where python on Windows) to list every match in search order, and python3 -c "import sys; print(sys.executable)" to see which one ran. Then call python3.12 --version (or py -3.12 --version) to confirm the new install works.
Exercise
Why is python3 -m pip --version a better check than pip --version?
Show answer
python3 -m pip runs the pip module of the exact interpreter named in the command, so its output and any later installs are tied to that Python. A bare pip command is found through PATH and may belong to a different Python version.
Exercise
What does the script check_python.py do when the interpreter is older than MINIMUM, and why is that useful in automation?
Show answer
It raises SystemExit with a message, which prints the message to standard error and exits with status 1. A shell script or CI job can see the nonzero status and stop, instead of continuing with an unsupported interpreter.