Changing the active Python interpreter on Windows

A lot of Windows Python software use wrapper .exe files which pick the Python interpreter  based on this registry settings. One notable exe is buildout.exe, which is used to run buildout.

If you install multiple Pythons, the latter installations might not become active in the registry automatically, and your Python wrapper still rely on the old version. This leads to version incompatibilities and you are unable to start the Python applications.

Since only one Python interpreter can be activate at a time, it is little bit difficult to develop multi-version Python code Windows. This kind of situation could be that you develop Plone 3 sites (Python 2.4) and Plone 4 sites (Python 2.6) simultaneously.

Below is regpy.py code which changes the active Python interpreter. The orignal author is unknown, I picked up this code from some paste board long time ago. Just run this code with your Python and the running interpreter becomes active.

Example how to activate alternative Python interpreter:

C:\Plone\python\python.exe regpy.py

regpy.py:

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()

\"\" Subscribe to RSS feed Follow me on Twitter Follow me on Facebook Follow me Google+

2 thoughts on “Changing the active Python interpreter on Windows

  1. I recently did this by opening the add/remove programs control panel and selecting ‘change’ for the given python version. It allowed me to re-enable the ‘register extensions’ option that I left disabled during install (because I wanted another Python version to remain the default).
    After completing the installer actions the new Python had become the default.

Leave a Reply

Your email address will not be published. Required fields are marked *