如何使用Python在注册表中找到Visual Studio的路径?

Nic*_*ton 2 registry visual-studio

我们有这个代码,但它不再起作用了:

def get_vcvarsall(generator):
 value = None
 type = None
 key_name = r'SOFTWARE\Microsoft\VisualStudio\SxS\VC7'
 key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, key_name)
 if generator.startswith('Visual Studio 8'):
  value,type = _winreg.QueryValueEx(key, '8.0')
 elif generator.startswith('Visual Studio 9'):
  value,type = _winreg.QueryValueEx(key, '9.0')
 elif generator.startswith('Visual Studio 10'):
  value,type = _winreg.QueryValueEx(key, '10.0')
 else:
  raise Exception('Cannot determin vcvarsall.bat location for: ' + generator)
 path = value + 'vcvarsall.bat'
 if not os.path.exists(path):
  raise Exception("'%s' not found.")
 return path
Run Code Online (Sandbox Code Playgroud)

这似乎已经停止工作,因为我从x86升级到Python 2.6 x64(但我不能确定).本来可以升级到导致问题的Win7.

Stu*_*Stu 5

这是x64的一部分.

由于Visual Studio是一个32位应用程序,它的注册表项在32位WoW dungeon中被推入.你会想看看

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\SxS\VS7
Run Code Online (Sandbox Code Playgroud)

请注意,如果您将Python作为32位可执行文件运行,它也会被重定向 - 所以一切都"正常".只有当您从64位应用程序中查找32位信息时,或者反之亦然,才会遇到问题.