11

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

user1219144
  • 111
  • 1
  • 3

2 Answers2

11

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))
Denis
  • 145
  • 1
  • 8
  • I was having a problem where my sub keys would not enumerated. Adding the KEY_WOW64_64KEY like you said solved my issue. Thank you. – FernandoZ Sep 30 '15 at 22:45
6

Are you also using a 64-bit version of Python, or is it a 32-bit Python? (The latter is more common.) If you're using a 32-bit version of Python, the _winreg module will see the 32-bit registry by default, while regedit will show you the 64-bit one. You should be able to tell _winreg to open a different view; see the _winreg module docs on access rights, specifically the subsection on 64-bit specific flags and the MSDN article it references. Unfortunately it doesn't look like there's a way for a 32-bit process to access the 64-bit registry, but I may be missing something.

Thomas Wouters
  • 130,178
  • 23
  • 148
  • 122
  • Thank you for your reply. When I check which Python version I have (using sys.version_info) it says (2, 6, 4, 'final', 0). I can't see any 64bit or 32bit info though. And the other thing is I don't know where to use the "KEY_WOW64_64KEY" part. I know, I probably sound pathetic but I do need help... – user1219144 Feb 20 '12 at 00:56
  • You can't see 32-versus-64-bit in `sys.version_info`, but you can see it in `sys.version` (it'll say '32 bit' or '64 bit' in there.) You can also look at `platform.architecture()`. As for opening the registry differently, as I said I don't believe you can access the 64-bit registry from a 32-bit process the way you can access either from a 64-bit process. I may be wrong, however; I've never done much with 32-bit registry access. – Thomas Wouters Feb 20 '12 at 10:12
  • @ThomasWouters : I'm also unable to read keys with `_winreg` on _Windows 7 64-bit_ & _python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)]_. I get the same error as @user1219144 _system cannot find the specified file_. To test this, I'm using this [WindowsRegistry class](http://is.gd/8DV8KL) with testcases. Behavior I see: **1st** run ok; **2nd** run ok; **After Reboot** error. To debug: try running _IDLE_ as Admin by putting `START C:\Python27\pythonw.exe C:\Python27\Lib\idlelib\idle.py` into a **.bat** file, right clicking it then choosing _Run as administrator_ – TrinitronX May 16 '12 at 04:54
  • ... then open the `WindowsRegistry.py` class, and run the testcases by pressing **F5** – TrinitronX May 16 '12 at 05:00
  • @TrinitronX: I am unable to read any "HKEY_LOCAL_MACHINE" keys on my system using _winreg on Windows 7 64 bit & python 2.7.3 (64bit). Something as simple as `_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, "\SOFTWARE\Microsoft\Microsoft SDKs\Windows")` doesn't work. I tried all possible WOW flags too. Tried it on a work system and home system. Tried it with Python 3.3. I have no idea what's going wrong. Works fine if i try to access HKEY_CLASSES_ROOT stuff. – Vikas Bhargava Nov 19 '13 at 11:54
  • @VikasBhargava: Please see my solved question about [accessing the windows registry with python here](http://stackoverflow.com/q/15030033/645491) – TrinitronX Nov 20 '13 at 18:15