Adding Python Information to the Windows Registry
February 19, 2003 | Fredrik Lundh
Some Python distributions add information to the Windows registry when installed. This information is used by certain tools, such as the win32all installer and Windows installers generated by the distutils package.
If you’re using an unregistered Python environment, you’ll usually end up with an empty list of alternatives on the installer’s “Select Python installation to use” screen.
The following script registers the current interpreter. The script should work for Python 2.0 and later.
Note that there can be only one registered interpreter for each major Python release (e.g. 2.0, 2.1, 2,2 etc).
# # script to register Python 2.0 or later for use with win32all # and other extensions that require Python registry settings # # written by Joakim Löw for Secret Labs AB / PythonWare # # source: # http://www.pythonware.com/products/works/articles/regpy20.htm import sys from _winreg import * # tweak as necessary version = sys.version[:3] installpath = sys.prefix regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version) installkey = "InstallPath" pythonkey = "PythonPath" pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % ( installpath, installpath, installpath ) def RegisterPy(): try: reg = OpenKey(HKEY_LOCAL_MACHINE, regpath) except EnvironmentError: try: reg = CreateKey(HKEY_LOCAL_MACHINE, regpath) SetValue(reg, installkey, REG_SZ, installpath) SetValue(reg, pythonkey, REG_SZ, pythonpath) CloseKey(reg) except: print "*** Unable to register!" return print "--- Python", version, "is now registered!" return if (QueryValue(reg, installkey) == installpath and QueryValue(reg, pythonkey) == pythonpath): CloseKey(reg) print "=== Python", version, "is already registered!" return CloseKey(reg) print "*** Unable to register!" print "*** You probably have another Python installation!" if __name__ == "__main__": RegisterPy()
(To download, triple-click on the first line to select the entire script, and copy the text into your favourite text editor.)
Unregistering the interpreter
If you need to reverse the above, you can add the following function to the script (code provided by Martin Lamar):
def UnRegisterPy(): try: reg = OpenKey(HKEY_LOCAL_MACHINE, regpath) except EnvironmentError: print "*** Python not registered?!" return try: DeleteKey(reg, installkey) DeleteKey(reg, pythonkey) DeleteKey(HKEY_LOCAL_MACHINE, regpath) except: print "*** Unable to un-register!" else: print "--- Python", version, "is no longer registered!"
Comment:
I correct pythonpath variable in this way to make it working pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % ( installpath, installpath, installpath )
Posted by Michele Cabano (2007-01-26)
Comment:
From the script that is supposed to set the registry (with a couple of prints added):
print version, installpath
2.4 C:\PYTHON24
print installpath, pythonpath
C:\PYTHON24 C:\PYTHON24;C:\PYTHON24Lib\;C:\PYTHON24DLLs\
Traceback (most recent call last):
File "register_python_version_that_runs_this_script.py", line 54, in ? RegisterPy()
File "register_python_version_that_runs_this_script.py", line 42, in RegisterPy
if QueryValue(reg, installkey) == installpath :
WindowsError: [Errno 2] The system cannot find the file specified
Neither key is in the registry for 2.2 (which PIL1.6 for 2.2 found and installed for), nor for 2.4. Both Active State Pythons are on the machine, but 2.4 is the .py-associated one and in the PATH, etc. etc.
Posted by Felix (2007-01-28)
Comment:
Ok, so here is how you can get around this error when registering Python: Change the top part to look like this:
# tweak as necessary version = sys.version installpath = sys.prefix regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version) installkey = "InstallPath" pythonkey = "PythonPath" pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % ( installpath, installpath, installpath )
Then when the script runs successfully you will need to go to the registry key (use regedit) and rename the one that has your version with (probably a really long name) and a couple other keys attached for the paths to "2.3". If there is a key already labeled "2.3" delete if empty, if it's not empty and has the path keys in it you should be good. Either way you will want to end up with one good registry key labeled "2.3" that has two keys with the paths to your python install.
Re-run the windows installer and it should find it.
Posted by Mark (2007-07-03)
Comment:
The "pythonpath" var in the register script has two errors. The correct code is: pythonpath = "%s;%s\Lib\\;%s\DLLs\\" % (installpath, installpath, installpath)
Posted by bart hubbard (2007-01-03)