This code used to work on Vista (and Windows XP) but after an upgrade to Windows 7 it now fails with the error shown:
Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32
>>> import _winreg
>>> h1 = _winreg.ConnectRegistry(None, _winreg.HKEY_LOCAL_MACHINE)
>>> key = r'SOFTWARE\Python\PythonCore\2.6\InstallPath'
>>> h2 = _winreg.OpenKey(h1, key, 0, _winreg.KEY_ALL_ACCESS)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
WindowsError: [Error 5] Access is denied
I'm fairly sure this is the result of changes in the security model in Windows 7, but various searches I tried have turned up nothing I can use as an answer so far.
(Not that it should be relevant, but to stave off "why would you do that?" responses, this is for a developer's utility which can switch the registry between multiple installations of Python, for use in a multi-project environment where we need more control over which version of Python is in use, and what packages are available, than things like virtualenv can provide.)
Edit: The logged-in user is an Administrator. Also, I've turned off the UAC (User Access Control) stuff as completely as one can (not true... see next edit), as was previously the case before the upgrade from Vista to Windows 7.
Edit 2: As noted in my own answer below, I hadn't rebooted after turning off UAC, so it was still set to the default. Apparently this results in the Access denied error (as I confirmed by testing with UAC set to Default and to Never).