After successfully installing Python, the crucial next step is verifying that it works as expected and understanding the nuances of how it is invoked from your command line. This process is often complicated by the presence of multiple Python versions on a single system, leading to the common python vs. python3 confusion.

Verifying the Installation

To confirm Python is installed correctly and accessible from your command-line interface (CLI), open your terminal (Command Prompt on Windows, Terminal on macOS/Linux) and execute a version check command. This command does two things: it confirms the interpreter is found in your system’s PATH and reports its version number.

python --version

However, the output of this command is where the potential for confusion begins. On systems where both Python 2 and Python 3 are present, the python command is often symlinked to Python 2 for historical reasons, as Python 2 was the default for many years. You might see an output like Python 2.7.18. This is why the python3 command was introduced—to provide an unambiguous way to invoke Python 3.

python3 --version

This should return something like Python 3.12.1, confirming that Python 3 is available. It is a best practice to use python3 explicitly in your scripts and command-line instructions to avoid any accidental reliance on a legacy Python 2 installation.

The python vs. python3 Conundrum

The existence of these two commands is not a formal part of the Python language but rather a convention adopted by operating system package managers and installers to manage coexistence. The python command is typically a symlink (on Unix-like systems) or a copy of the executable. Which version it points to depends entirely on your specific system configuration. On a fresh Windows install using the official installer from python.org, the python command will usually point to the newly installed Python 3 version. On many Linux distributions and macOS systems, python remains Python 2 unless you explicitly change it, which is not recommended as it can break system tools that depend on Python 2.

To see exactly what is happening on your system, you can use the which command (on macOS/Linux) or where command (on Windows) to locate the executable.

# macOS/Linux: find which 'python' points to
which python
which python3

# Windows: find the location of the executable
where python
where python3

Launching the Python Interpreter

You can start an interactive Python REPL (Read-Eval-Print Loop) session using either command. The key is to know which version you are launching. The prompt will subtly indicate this: Python 2.x prompts typically look like >>>, while Python 3.x prompts look the same but the introductory text will clearly state the version number.

# This might start Python 2 or 3, depending on your system
python

# This should unequivocally start Python 3
python3

Once in the REPL, you can run a simple statement to verify it’s working correctly.

print("Hello, World!")

Executing a Python Script

The same principle applies when running script files. You must be intentional about which interpreter executes your code. A script written with Python 3 syntax (e.g., using print as a function) will fail under a Python 2 interpreter.

# Run my_script.py with the default 'python' interpreter
python my_script.py

# Run my_script.py with the explicit Python 3 interpreter
python3 my_script.py

Best Practices and Pitfalls

  1. Explicit is Better Than Implicit: Always use python3 and pip3 in your documentation, scripts, and command-line instructions. This removes ambiguity and prevents errors when your code is run on a system with multiple Python versions.
  2. Check Your Version Programmatically: If you are writing a script that requires a specific minimum version of Python, you can check for it at runtime.
    import sys
    if sys.version_info < (3, 8):
        sys.exit("This script requires Python 3.8 or higher.")
    else:
        print("Python version is sufficient.")
    
  3. Shebang Line for Scripts: On Unix-like systems, include a shebang line at the very top of your executable Python scripts to define which interpreter should be used. Using #!/usr/bin/env python3 is the most portable and reliable method, as it uses the env command to find the python3 executable in the user’s PATH.
    #!/usr/bin/env python3
    print("This script will use python3!")
    
  4. Pitfall: Assuming python Points to Python 3: The most common mistake is assuming the python command on any given system will be version 3. Never assume. Always verify using python --version and use python3 for certainty.
  5. Virtual Environments are the Solution: The ultimate way to manage this confusion is by using virtual environments. A virtual environment isolates your project and its dependencies, and it creates unambiguous symlinks named python and pip that point to the specific Python version used to create the environment. This makes your commands consistent and project-specific.