0% found this document useful (0 votes)
500 views2 pages

Winreg Complete Example

This document provides a Python script to register a Python interpreter in the Windows registry. The script adds information about the Python installation path and Python path to allow tools like the win32all installer and distutils to find the correct Python installation. It first checks if the interpreter is already registered and only registers it if the paths do not match. The document also includes an additional function to unregister the Python interpreter by deleting the keys added to the registry.

Uploaded by

oraculus_lunae
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
500 views2 pages

Winreg Complete Example

This document provides a Python script to register a Python interpreter in the Windows registry. The script adds information about the Python installation path and Python path to allow tools like the win32all installer and distutils to find the correct Python installation. It first checks if the interpreter is already registered and only registers it if the paths do not match. The document also includes an additional function to unregister the Python interpreter by deleting the keys added to the registry.

Uploaded by

oraculus_lunae
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Source: http://effbot.org/zone/python-register.

htm

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 (dead link) package.

If youre using an unregistered Python environment, youll usually end up with an


empty list of alternatives on the installers 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).

Register a Python Interpreter


#
# script to register Python 2.0 or later for use with win32all
# and other extensions that require Python registry settings
#
# written by Joakim Lw 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):

Unregister a Python Interpreter


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!"

You might also like