How to use Python’s py launcher for Windows

Take control of multiple Python installations in Windows with the py launcher, available as part of a standard Python installation.

How to use Python’s py launcher for Windows
Thinkstock

The Windows version of the Python interpreter can be run from the command line the same way it’s run in other operating systems, by typing python or python3 at the prompt. But there’s a feature unique to the Python edition of Windows that makes it easier to manage multiple installed versions of the Python interpreter—the py launcher.

The py launcher—or just py for short—is a shortcut to all of the installed versions of Python on one’s system. With a command-line switch, you see at a glance all of the Python interpreters you’ve installed, and invoke a specific version of Python, whether 32-bit or 64-bit.

Install the py launcher

The py launcher is optionally installed during the setup process when you first install Python on Windows. At one point you’ll be prompted to do so, as per the screenshots shown below. There is no downside to having py installed. You can always run the Python interpreter directly instead, and py doesn’t interfere with other behaviors.

python install 01 IDG

When installing Python on Windows, select the “Customize installation” option during setup.

python install 02 IDG

In the next screen, make sure the “py launcher” option is checked.

When you type py at the command line, the launcher will invoke the current default Python interpreter. py by itself will drop you into the Python REPL, which you can exit as you normally would by typing quit() or Ctrl-Z.

To see which versions of Python are available to py, type py -0p. You’ll be presented with a list of all the known interpreters in the system, their version numbers, and their full paths. The interpreter marked with an asterisk is the default.

To invoke a specific edition of Python, type py followed by the switch in the left-hand column for the appropriate version. For instance, to launch the 64-bit edition of Python 3.9, you would type py -3.9-64.

Note that if you provide only a version number, and not a bitness indicator, you’ll default to whichever version of Python is native to your machine’s processor type. On a 64-bit machine, that would be the 64-bit edition. So if we just typed py -3.9, we’d get the 64-bit version of Python 3.9.

py examples IDG

Four examples of the py launcher in action.

If you don’t specify a bitness, and only one bitness of a particular version is installed, that bitness will be loaded by default. In the above example, if we typed py -3.10, we’d get the 64-bit version of Python 3.10, because only the 64-bit version is present.

If you use just -2 or -3 as the version switch, Python will launch the most recent version of Python 2.x or Python 3.x, as indicated.

Whenever a new version of Python is installed, the py launcher will be updated, as long as the Python version being installed is newer than the py launcher. For instance, if you installed Python 3.8 and then Python 3.9, the latter version would upgrade py. But if you upgraded your Python 3.8 installation afterward, py wouldn’t be touched since the installer would detect the existence of a newer version.

Run Python scripts with the py launcher

To run a Python script with the py launcher, simply substitute py and its command-line switches for python or python3. For instance, here is the command typically used to upgrade pip, by running it as a module:

python -m pip install -U pip

If we have the py launcher, we just type:

py -m pip install -U pip

To select a specific installation of Python, just pass the version as the first element in the argument list. Any arguments provided after the version are passed along as per usual.

py -3.9 -m pip install -U pip

Set the default Python for py launcher

If you want to ensure that a given Python instance runs by default when you run py, you can do so in a number of different ways. The methods are evaluated in this order:

  1. The active virtual environment. If you’re running py from a shell session where a Python virtual environment is active, the virtual environment’s edition of Python will be associated with py by default. You can always override this by providing a specific version switch.
  2. The shebang line in the script. Python scripts that begin with a line in the format #!/path/to/python python3 or #!"C:\Python3.3\python.exe" will be run with the interpreter specified there.
  3. The PY_PYTHON2 or PY_PYTHON3 environment variables, when using the -2 or -3 switch.
  4. The PY_PYTHON environment variable. If you set a version number (e.g., 3.9-64 or just 3.9) with either PY_PYTHON or the previously mentioned environment variables, py will default to launching that version.

Copyright © 2021 IDG Communications, Inc.

InfoWorld Technology of the Year Awards 2023. Now open for entries!