Wednesday 1 October 2008 — This is more than 16 years old. Be careful.
I forget what software first set up these associations, but I have .py files registered with Windows so that they can execute directly. The registry defines .py as a Python.File, which has a shell open command of:
"C:\Python24\python.exe" "%1" %*
My PATHEXT environment variable includes .py, so the command prompt will attempt to execute .py files, using the registry associations to find the executable.
But: I wanted to switch from Python 2.4 to Python 2.5. That meant updating the registry in a handful of places. A Python script to the rescue!
""" Change the .py file extension to point to a different
Python installation.
"""
import _winreg as reg
import sys
pydir = sys.argv[1]
todo = [
('Applications\python.exe\shell\open\command',
'"PYDIR\\python.exe" "%1" %*'),
('Applications\pythonw.exe\shell\open\command',
'"PYDIR\\pythonw.exe" "%1" %*'),
('Python.CompiledFile\DefaultIcon',
'PYDIR\\pyc.ico'),
('Python.CompiledFile\shell\open\command',
'"PYDIR\\python.exe" "%1" %*'),
('Python.File\DefaultIcon',
'PYDIR\\py.ico'),
('Python.File\shell\open\command',
'"PYDIR\\python.exe" "%1" %*'),
('Python.NoConFile\DefaultIcon',
'PYDIR\\py.ico'),
('Python.NoConFile\shell\open\command',
'"PYDIR\\pythonw.exe" "%1" %*'),
]
classes_root = reg.OpenKey(reg.HKEY_CLASSES_ROOT, "")
for path, value in todo:
key = reg.OpenKey(classes_root, path, 0, reg.KEY_SET_VALUE)
reg.SetValue(key, '', reg.REG_SZ, value.replace('PYDIR', pydir))
Invoke this with your desired Python installation directory, and the registry is updated to point to it.
Note that this doesn’t affect what the command “python” means, that’s determined by your PATH environment variable. These registry settings change which Python executable is found when you invoke a .py file as a command.
Comments
...although it's probably just as easy (if not easier) to simply use the script you posted.
As Brian said, there are symlinks in windows, which are very handy. I use NTFSLink (google it) to update mine (I use one at work as my current working SVN branch, so everything that references a path on disk can be swapped as I see fit, even stuff like recent projects in visual studio etc).
But, cool to see all the spots you need to update to fix those file associations in windows.
in explorer:
Tools> Folder Options> File Types
select py file extension, click advanced, and edit properties for action ``open``
In this case, change 24 to 25 in the executable path, this changes the default for execution.
This takes a few clicks and changing one number, instead of having to figure out which registry keys to change and risking that the windows registry gets screwed up (Which I would surely do, by the time I figure out how to change registry settings directly.)
And additionally, its easy to add actions through ``Folder Options``, I have (my default is still Python 2.4):
Edit with IDLE
Edit with IDLE no -n
Edit with IDLE py25
Edit with SPE
Edit with Wing
All these actions get automatically added to the context menu in windows explorer.
Josef
Josef
But thanks for the pointers!
On my windows machine one of the paths failed, so I had to modify the code to get it to work.
When I ran it as coded, I received: So I changed it to have a try block: Now I get:
I type the name of a python file at the cmd prompt, it opens in Python 2.5.
I right click on 'xxx.py', select properties, and click the 'opens with' button. I browse to the Python 2.7 executable & hit OK and Apply.
Weirdly, at this point, the properties dialog now displays the 'Opens with' as a very old Python icon. (it was previously the modern Python icon)
Sure enough, typing 'xxx.py' in cmd.exe now opens the script using Python 2.4. This is wrong - I expected 2.7.
Ned's script worked perfectly first time. Score one for using a script. :-)
classes_root = reg.OpenKeyEx(reg.HKEY_CLASSES_ROOT, "")
for path, value in todo:
key = reg.CreateKeyEx(classes_root, path, 0, reg.KEY_SET_VALUE)
reg.SetValue(key, '', reg.REG_SZ, value.replace('PYDIR', pydir))
CreateKeyEx opens an existing key or creates it if it doesn't exists. I'm on 64 bit Windows 7 Pro. Everything works fine now.
Add a comment: