Sub topic 6
Checking that Python is installed
This chapter assumes you can open a terminal and that you have already run an installer for Python 3. You will confirm that the interpreter is installed, find out which copy your shell runs, and diagnose the common failures.
The basic check
Open a new terminal window and ask the interpreter for its version.
python3 --version
# On Windows, use: py --version
# If your system maps python to Python 3, this also works: python --versionA working install prints a line such as Python 3.12.4. The first number must be 3. If you see Python 2.7.x, you are running an obsolete interpreter and should use the python3 command instead.
Open a new terminal after installing. A terminal that was already open keeps the PATH it started with and will not see the change.
What the shell actually runs
Your shell finds a command by searching the directories listed in PATH, in order, and running the first match. Several Pythons can be installed at once, so the version number alone does not tell you which copy ran. Ask the shell for the location.
| Platform | Command | Example output |
|---|---|---|
| macOS and Linux | `which python3` | `/usr/bin/python3` |
| Windows (Command Prompt) | `where python` | `C:\Users\you\AppData\Local\Programs\Python\Python312\python.exe` |
| Windows (launcher) | `py -0` | A list of installed versions |
You can also ask Python to report on itself. This works the same on every platform once you have a command that runs.
python3 -c "import sys; print(sys.executable); print(sys.version)"sys.executable is the full path of the running interpreter. sys.version is the full version string. If this path is not the one you expected, another install is earlier in PATH.
Reading the failures
Each failure message points to a different cause.
command not found(macOS and Linux) or'python3' is not recognized(Windows): the shell found no matching program in PATH. Either Python is not installed, or its directory is not in PATH.- On Windows, a Microsoft Store window opens, or the message says "Python was not found; run without arguments to install from the Microsoft Store". This comes from an app execution alias, a placeholder that stands in for Python. Install Python, or try
py --version. Python 2.7.x, or an older Python 3 than you installed: an older copy comes earlier in PATH. Usewhich -a python3on macOS and Linux, orwhere pythonon Windows, to list every match in search order.pythonworks butpython3does not, or the reverse: command names differ between systems. Use the name that prints a Python 3 version, and use it consistently.
Check pip too
Package installs come later in this tutorial. Confirm now that pip belongs to the same interpreter.
python3 -m pip --versionThe output ends with the path of the Python it is attached to, such as (python 3.12). Running pip as python3 -m pip guarantees it matches the interpreter you just checked. A bare pip command may belong to a different install.
Exercise
You run python3 --version and see Python 3.9.6, but you just installed Python 3.12. What is the most likely cause, and which commands would you use to confirm it?
Show answer
An older Python 3.9 is earlier in PATH, so the shell runs it first. Run which -a python3 on macOS or Linux, or where python on Windows, to list all matches in order. Then run python3 -c "import sys; print(sys.executable)" to see which one is running. Fix it by putting the 3.12 directory earlier in PATH, or by calling it with its full path or through the py launcher.
Exercise
Why is python3 -m pip --version more reliable than pip --version for checking your setup?
Show answer
The -m form runs pip as a module of the exact interpreter named in the command, so pip and Python always match. A bare pip is found through PATH and may belong to a different Python install.