What is the difference between 'py' and 'python' in the Windows terminal?

Learn what is the difference between 'py' and 'python' in the windows terminal? with practical examples, diagrams, and best practices. Covers python, windows, pip development techniques with visual...

Understanding 'py' vs. 'python' in the Windows Terminal

Hero image for What is the difference between 'py' and 'python' in the Windows terminal?

Explore the key differences between using 'py' and 'python' commands in the Windows Command Prompt or PowerShell, and learn how to manage multiple Python versions effectively.

When working with Python on Windows, you might encounter two common commands for invoking the Python interpreter: python and py. While they often seem interchangeable, there are crucial differences in how they operate, especially when you have multiple Python versions installed. Understanding these distinctions is key to managing your Python environments efficiently and avoiding unexpected behavior in your scripts and development workflows.

The 'python' Command: Relying on PATH

The python command, when executed in the Windows terminal, directly invokes the Python executable found first in your system's PATH environment variable. This means that if you have multiple Python installations (e.g., Python 3.8, Python 3.9, Anaconda, etc.), the python command will always point to the one whose installation directory appears earliest in your PATH.

This behavior can lead to inconsistencies if you're not careful. For instance, if your PATH points to Python 3.8, but your project requires Python 3.9, simply typing python will run your script with the wrong interpreter, potentially causing errors due to missing modules or syntax incompatibilities.

python --version
# Output might be: Python 3.8.5

where python
# Output shows the full path to the python.exe being used, e.g.:
# C:\Python38\python.exe

Checking the Python version and location invoked by the python command.

flowchart TD
    A[User types 'python'] --> B{Search PATH Environment Variable}
    B --> C{Find first 'python.exe' entry}
    C --> D[Execute that specific Python interpreter]
    D --> E{Potential issue: May not be desired version}

How the 'python' command resolves the interpreter based on the system PATH.

The 'py' Command: The Python Launcher for Windows

The py command, on the other hand, is not a direct executable but rather a special utility called the Python Launcher for Windows. This launcher is typically installed alongside Python (starting from Python 3.3) and acts as a smart dispatcher. Its primary purpose is to help you easily select and run specific Python versions, especially when you have several installed.

The Python Launcher works by looking for shebang lines (e.g., #!python3.9) at the top of your Python scripts. If a shebang is present, it will launch the specified Python version. If no shebang is found, or if it's generic (e.g., #!python), it defaults to the latest Python 3 version installed on your system, or the latest Python 2 if explicitly requested or if no Python 3 is found.

This makes py a much more flexible and robust way to run Python scripts and interact with different Python environments without constantly modifying your system's PATH.

py --version
# Output might be: Python 3.9.7 (or the latest Python 3 installed)

py -3.8 script.py
# Runs script.py using Python 3.8

py -3.9
# Launches the Python 3.9 interpreter directly

Using the py command to specify Python versions.

Key Differences and Best Practices

The fundamental difference lies in their resolution mechanism: python relies on the system PATH, while py uses the Python Launcher's internal logic and shebang lines. This distinction has significant implications for managing your Python installations.

When to use python:

  • When you are certain that the python command in your PATH points to the exact version you need for a global utility or a specific environment.
  • Inside virtual environments, where the python command within the activated environment's Scripts directory will correctly point to the virtual environment's interpreter.

When to use py:

  • When you have multiple Python versions installed and need to explicitly choose one (e.g., py -3.9).
  • When running Python scripts that include a shebang line to specify the interpreter.
  • As a general best practice for launching Python on Windows, as it provides more control and reduces PATH-related conflicts.
graph TD
    subgraph "'python' Command"
        A[User types 'python'] --> B{System PATH Lookup}
        B --> C["First 'python.exe' found"]
        C --> D[Execute]
    end

    subgraph "'py' Command (Python Launcher)"
        E[User types 'py'] --> F{Check Shebang Line in Script}
        F -->|Specific Version (e.g., #!python3.9)| G[Launch Specified Version]
        F -->|No Shebang or Generic| H[Launch Latest Python 3]
    end

    style A fill:#f9f,stroke:#333,stroke-width:2px
    style E fill:#ccf,stroke:#333,stroke-width:2px

Comparison of how 'python' and 'py' commands resolve the Python interpreter.

Managing Python with Virtual Environments

Regardless of whether you use py or python, virtual environments (venv or conda) are crucial for managing project-specific dependencies. When a virtual environment is activated, its Scripts directory is temporarily prepended to your PATH. This ensures that the python command (and pip) within that terminal session refers specifically to the interpreter and packages of the active environment, overriding any global PATH settings or the py launcher's default behavior for that session.

1. Create a Virtual Environment

Navigate to your project directory and create a virtual environment using py -3.9 -m venv .venv (or your desired Python version).

2. Activate the Environment

Activate the environment using .venv\Scripts\activate in PowerShell or .\.venv\Scripts\activate in Command Prompt.

3. Use 'python' and 'pip' within the Environment

Once activated, python and pip commands will now refer to the interpreter and package manager within your virtual environment, ensuring project isolation.