views:

654

answers:

2

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

A: 

I think you have an access right problem.

Try to open the key with a less demanding access right (e.g. KEY_QUERY_VALUE) and check if it works. Of course, with that change you will not be able to change the registry, but it would be only for pinpointing the issue.
As an alternative, try to execute the utility from a user with higher privileges - and by the way this would be the only solution I could see for a problem involving access rights.

Roberto Liffredo
Certainly it works with lesser rights. KEY_WRITE fails, KEY_READ and KEY_QUERY_VALUE work. It also worked fine on the same machine just before upgrading from Vista to Windows 7. The user (me) has always had administrative rights, and I'll update the question to reflect that and the fact that I turned UAC right off fully, as I had in Vista before the upgrade.
Peter Hansen
Can you change that value by using regedit?
Roberto Liffredo
+1  A: 

This was a user mistake, compounded or triggered by changes in Windows 7 to how the UAC feature is implemented.

In Vista, the much-detested User Access Control feature was binary, either on or off. On Windows 7 that has been changed to provide four levels of granularity:

  • Always Notify (when either programs or user tries to change settings)
  • Default (notify only when programs try to make changes, and dim screen)
  • Notify without dimming (same as default but don't dim screen when notifying)
  • Never notify (for either programs or user changes)

My mistake was in not rebooting after dropping the UAC feature down to the Never Notify level. (Vista was aggressive about requesting that you reboot, while Windows 7 seems to be slightly more passive.)

Peter Hansen