Complete News World in United States

How to use Python’s py launcher for Windows

The Home windows model of the Python interpreter will be run from the command line the identical means it’s run in different working methods, by typing python or python3 on the immediate. However there’s a characteristic distinctive to the Python version of Home windows that makes it simpler to handle a number of put in variations of the Python interpreter—the py launcher.

The py launcher—or simply py for brief—is a shortcut to all the put in variations of Python on one’s system. With a command-line swap, you see at a look all the Python interpreters you’ve put in, and invoke a selected model of Python, whether or not 32-bit or 64-bit.

Set up the py launcher

The py launcher is optionally put in throughout the setup course of while you first set up Python on Home windows. At one level you’ll be prompted to take action, as per the screenshots proven under. There is no such thing as a draw back to having py put in. You possibly can at all times run the Python interpreter instantly as an alternative, and py doesn’t intrude with different behaviors.

python install 01 IDG

When putting in Python on Home windows, choose the “Customise set up” possibility throughout setup.

python install 02 IDG

Within the subsequent display, be certain that the “py launcher” possibility is checked.

While you kind py on the command line, the launcher will invoke the present default Python interpreter. py by itself will drop you into the Python REPL, which you’ll be able to exit as you usually would by typing give up() or Ctrl-Z.

To see which variations of Python can be found to py, kind py -0p. You’ll be offered with a listing of all of the identified interpreters within the system, their model numbers, and their full paths. The interpreter marked with an asterisk is the default.

To invoke a selected version of Python, kind py adopted by the swap within the left-hand column for the suitable model. For example, to launch the 64-bit version of Python Three.9, you’ll kind py -Three.9-64.

Word that if you happen to present solely a model quantity, and never a bitness indicator, you’ll default to whichever model of Python is native to your machine’s processor kind. On a 64-bit machine, that will be the 64-bit version. So if we simply typed py -Three.9, we’d get the 64-bit model of Python Three.9.

py examples IDG

4 examples of the py launcher in motion.

In the event you don’t specify a bitness, and just one bitness of a selected model is put in, that bitness can be loaded by default. Within the above instance, if we typed py -Three.10, we’d get the 64-bit model of Python Three.10, as a result of solely the 64-bit model is current.

In the event you use simply -2 or -Three because the model swap, Python will launch the latest model of Python 2.x or Python Three.x, as indicated.

Each time a brand new model of Python is put in, the py launcher can be up to date, so long as the Python model being put in is newer than the py launcher. For example, if you happen to put in Python Three.eight after which Python Three.9, the latter model would improve py. However if you happen to upgraded your Python Three.eight set up afterward, py wouldn’t be touched because the installer would detect the existence of a more recent model.

Run Python scripts with the py launcher

To run a Python script with the py launcher, merely substitute py and its command-line switches for python or python3. For example, right here is the command usually used to improve pip, by working it as a module:

python -m pip set up -U pip

If we now have the py launcher, we simply kind:

py -m pip set up -U pip

To pick out a selected set up of Python, simply go the model as the primary aspect within the argument listing. Any arguments supplied after the model are handed alongside as per standard.

py -Three.9 -m pip set up -U pip

Set the default Python for py launcher

If you wish to be sure that a given Python occasion runs by default while you run py, you are able to do so in plenty of other ways. The strategies are evaluated on this order:

  1. The energetic digital surroundings. In the event you’re working py from a shell session the place a Python digital surroundings is energetic, the digital surroundings’s version of Python can be related to py by default. You possibly can at all times override this by offering a selected model swap.
  2. The shebang line within the script. Python scripts that start with a line within the format #!/path/to/python python3 or #!"C:Python3.3python.exe" can be run with the interpreter specified there.
  3. The PY_PYTHON2 or PY_PYTHON3 surroundings variables, when utilizing the -2 or -Three swap.
  4. The PY_PYTHON surroundings variable. In the event you set a model quantity (e.g., Three.9-64 or simply Three.9) with both PY_PYTHON or the beforehand talked about surroundings variables, py will default to launching that model.

Copyright © 2021 IDG Communications, Inc.