Python _winreg woes

user1219144 picture user1219144 · Feb 19, 2012 · Viewed 7.8k times · Source

I'm trying to access the windows registry (in Python) to query a key value using _winreg and I can't get it to work. The following line returns a WindowsError saying that the "system cannot find the specified file":

key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\Autodesk\Maya\2012\Setup\InstallPath', 0, _winreg.KEY_ALL_ACCESS)

After hours of trying, it looks like Python cannot see beyond the "Maya" part of the path (it looks like the "2012\...etc..." sub-path is "invisible" or non-existent). Now I have the Registry Editor open and I guaranty there IS such a path in the HKLM. I'm on Windows 7 64bit. Any idea what I'm doing wrong? This is driving me nuts. Thanks...

Answer

Denis picture Denis · May 29, 2013

You need to combine the access key with one of the 64bit access keys.

_winreg.KEY_WOW64_64KEY Indicates that an application on 64-bit Windows should operate on the 64-bit registry view.

_winreg.KEY_WOW64_32KEY Indicates that an application on 64-bit Windows should operate on the 32-bit registry view.

Try:

_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\Autodesk\Maya\2012\Setup\InstallPath', 0, (_winreg.KEY_WOW64_64KEY + _winreg.KEY_ALL_ACCESS))