Welcome, guest | Sign In | My Account | Store | Cart

One problem with ending all of your python scripts in .py is that you have to type the full name of the command. On Unix you can symlink to it with the shortened name, or wrap it in a bourne-shell script, or a shell alias. None of these solutions is particularly clean.

On MS Windows, file extensions have much more meaning. In fact, there is an environment variable to control what extensions should be considered executables, to permit them to be run from name, without the extension. This is the PATHEXT environment variable. Add .PY to this, and all of your python scripts can be executed transparently.

Python, 20 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
My original PATHEXT variable. 

M:\>echo %PATHEXT%
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH

Using Control Panel -> System -> Advanced -> Environment Variables, I edited this and added .PY.

M:\>echo %PATHEXT%
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.PY

I have M:\bin in my PATH as well, so now DOS just finds the commands.

M:\>type bin\pytest.py

import sys

print "Hello from MS Windows:", sys.getwindowsversion()

M:\>pytest
Hello from MS Windows: (5, 1, 2600, 2, 'Service Pack 2')

I'm not MS Windows' biggest fan. This makes it more bearable. I might even take the time to put together a good suite of Unix-like tools in Python that will then just work when I install Python and make this mod, not to mention all of the other possibilities for crawling the registry, and basic system administration.

This is end-user computing, providing desperately needed hacker goodness to an otherwise lousy environment for Unix geeks.

Created by Michael Soulier on Thu, 30 Mar 2006 (PSF)
Python recipes (4591)
Michael Soulier's recipes (2)

Required Modules

  • (none specified)

Other Information and Tasks