The Home windows model of the Python interpreter can be run from the command line the similar way it’s run in other working methods, by typing
python3 at the prompt. But there is a function distinctive to the Python version of Home windows that helps make it much easier to handle several mounted versions of the Python interpreter—the py launcher.
The py launcher—or just py for short—is a shortcut to all of the mounted versions of Python on one’s method. With a command-line change, you see at a glance all of the Python interpreters you have mounted, and invoke a unique model of Python, no matter if 32-little bit or sixty four-little bit.
Install the py launcher
The py launcher is optionally mounted all through the setup system when you first set up Python on Home windows. At just one point you’ll be prompted to do so, as for each the screenshots shown below. There is no downside to possessing py mounted. You can generally run the Python interpreter straight alternatively, and py doesn’t interfere with other behaviors.
When you kind
py at the command line, the launcher will invoke the current default Python interpreter.
py by itself will fall you into the Python REPL, which you can exit as you usually would by typing
give up() or Ctrl-Z.
To see which versions of Python are obtainable to py, kind
py -0p. You are going to be introduced with a list of all the regarded interpreters in the method, their model figures, and their complete paths. The interpreter marked with an asterisk is the default.
To invoke a unique version of Python, kind
py adopted by the change in the still left-hand column for the appropriate model. For instance, to launch the sixty four-little bit version of Python 3.nine, you would kind
py -3.nine-sixty four.
Be aware that if you offer only a model range, and not a bitness indicator, you’ll default to whichever model of Python is indigenous to your machine’s processor kind. On a sixty four-little bit machine, that would be the sixty four-little bit version. So if we just typed
py -3.nine, we’d get the sixty four-little bit model of Python 3.nine.
If you do not specify a bitness, and only just one bitness of a particular model is mounted, that bitness will be loaded by default. In the previously mentioned instance, if we typed
py -3.10, we’d get the sixty four-little bit model of Python 3.10, for the reason that only the sixty four-little bit model is current.
If you use just
-3 as the model change, Python will launch the most the latest model of Python 2.x or Python 3.x, as indicated.
Anytime a new model of Python is mounted, the py launcher will be updated, as long as the Python model being mounted is newer than the py launcher. For instance, if you mounted Python 3.eight and then Python 3.nine, the latter model would up grade py. But if you upgraded your Python 3.eight installation afterward, py would not be touched because the installer would detect the existence of a newer model.
Run Python scripts with the py launcher
To run a Python script with the py launcher, simply just substitute
py and its command-line switches for
python3. For instance, listed here is the command normally used to up grade
pip, by functioning it as a module:
python -m pip set up -U pip
If we have the py launcher, we just kind:
py -m pip set up -U pip
To select a unique installation of Python, just go the model as the first component in the argument list. Any arguments supplied soon after the model are handed alongside as for each regular.
py -3.nine -m pip set up -U pip
Set the default Python for py launcher
If you want to assure that a presented Python instance runs by default when you run py, you can do so in a range of various strategies. The strategies are evaluated in this order:
- The active virtual natural environment. If you’re functioning py from a shell session where a Python virtual natural environment is active, the virtual environment’s version of Python will be connected with py by default. You can generally override this by offering a unique model change.
- The shebang line in the script. Python scripts that start with a line in the format
#!"C:Python3.3python.exe"will be run with the interpreter specified there.
PY_PYTHON3natural environment variables, when using the
PY_PYTHONnatural environment variable. If you set a model range (e.g.,
3.nine-sixty fouror just
3.nine) with either
PY_PYTHONor the earlier mentioned natural environment variables, py will default to launching that model.
Copyright © 2021 IDG Communications, Inc.