I'm fairly new to programming/coding, so bear with me.
I noticed that when I run .py files in cmd, the command looks something like:
C:\Users\Desktop\hello
"hello"
with hello being the python file, and the hello string being output.
For my friend, who's learning code along side me, the command looks more like:
C:\Users\Desktop\python hello.py
"hello"
My question is, what is the explanation for why my command is shorter as opposed to my friends? Can I help get my friends commands to look more like mine? I would really love some clarity here. Thanks!
2 Answers 2
The Python installer for Windows has the option to register file extensions. If registered, python is not required to be typed, just the extension, like so:
c:\> script.py
The PATHEXT environment variable has a list of extensions to try if not present on a command. It typically looks like:
C:\>set pathext
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
The installer doesn't update this, but if you edit and add typical Python extensions you don't have to type the extension. To find the editor in Windows 10, search for "edit environment" in the taskbar.
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.py;.pyw
You can run some commands to see if Python is registered for Python scripts:
C:\>assoc .py
.py=Python.File
C:\>ftype Python.File
Python.File="C:\WINDOWS\py.exe" "%L" %*
Above shows that py.exe (the Python Launcher) is associated with the .py extension. It is another installer option that give more options for launching scripts, like specifying what version of python to run if you have more than one installed. See PyLauncher for more details.
1 Comment
According to your example your just chose a longer name for their python file. yours = 'hello.py' friends = 'python hello.py'
inside each of the files you will find the same print statement that is actually responsible for the output
print("hello")
ftype /?andassoc /?. Thenpath /?.set pathext.