How Does py Launch Python3 on Windows Command Prompt

2

Im trying to understand how python get's launched from the command prompt with the "py" syntax. From my understanding, an environment variable is created, allowing python.exe to be executed from the cmd Users path but when I look at the Path Variables I dont see anything with py as a variable name. Can someone explain, on a high level, how Windows derives the py variable to allow it to execute python 3?

Jack

Posted 2019-10-03T02:41:16.023

Reputation: 21

Answers

1

py is not an environment variable, it's an executable that, for me at least, is located in your Windows directory (C:\Windows\ for me).

So, enter py or py.exe in your command prompt, and Python Launcher will run Python.

As you probably know, when you enter something into the command prompt, it will search all the directories in the PATH environment variable for a matching executable. It will execute the first one it finds, and stop the search. The Windows directory is in your PATH.

Help output for py.exe:

C:\Users\Admin>py -h
Python Launcher for Windows Version 3.7.2150.1013

usage:
py [launcher-args] [python-args] script [script-args]

Launcher arguments:

-2     : Launch the latest Python 2.x version
-3     : Launch the latest Python 3.x version
-X.Y   : Launch the specified Python version
     The above all default to 64 bit if a matching 64 bit python is present.
-X.Y-32: Launch the specified 32bit Python version
-X-32  : Launch the latest 32bit Python X version
-X.Y-64: Launch the specified 64bit Python version
-X-64  : Launch the latest 64bit Python X version
-0  --list       : List the available pythons
-0p --list-paths : List with paths

[...]

GordonAitchJay

Posted 2019-10-03T02:41:16.023

Reputation: 309

Thanks for this answer, path variables have always been a confusing subject. Its nice to get an idea of what happens in the back end! – Jack – 2019-12-11T17:48:15.277