在 Python 中获取所有已安装 Windows 程序的“正确”和可靠方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2077902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:46:23  来源:igfitidea点击:

'proper' & reliable way to get all installed windows programs in Python?

pythonwindowsregistrywmiwin32com

提问by ThantiK

I've seen numerous ways of retrieving installed programs on WinXP+ in python. What is the properand most robustway of doing this?

我已经看到了多种在 Python 中检索 WinXP+ 上已安装程序的方法。这样做的正确最可靠的方法是什么?

Currently I'm accessing HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstalland reading each of the keys from there to get a list. (I've been told this isn't the properway of doing things) I've seen examples of using WMI/Win32com to do this as well but have seen comments along with those implementations that WMI might be turned off on certain machines and that it's not a very reliable solution.

目前,我正在访问HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall和读取每个键以获取列表。(有人告诉我这不是正确的做事方式)我也看到了使用 WMI/Win32com 来执行此操作的示例,但也看到了评论以及可能在某些机器上关闭 WMI 的那些实现以及这不是一个非常可靠的解决方案。

Is there a method which is both proper, and reliable to get a list of installed programs? None of the WMI examples I've seen have worked on this machine (hence my reluctance to use it, I'm only running WinFLP; which is a stripped vers of XP.)

是否有一种既正确又可靠的方法来获取已安装程序的列表?我见过的 WMI 示例都没有在这台机器上工作(因此我不愿意使用它,我只运行 WinFLP;这是 XP 的一个剥离版本。)

I seem to have also found the TechNet article which my searches have turned up which is provided to a similar answer on my question: http://gallery.technet.microsoft.com/ScriptCenter/en-us/154dcae0-57a1-4c6e-8f9f-b215904485b7Note that Vista/7 listed under Platforms very clearly says "Nope"...won't work. So the WMI deal seems like it's a no-go...

我似乎还找到了我的搜索结果的 TechNet 文章,该文章提供了对我的问题的类似答案:http: //gallery.technet.microsoft.com/ScriptCenter/en-us/154dcae0-57a1-4c6e- 8f9f-b215904485b7请注意,平台下列出的 Vista/7 非常清楚地说“不”......不会工作。因此,WMI 交易似乎是行不通的……

Being able to retrieve the installed path would be an upside as well, as right now my current code does not account for someone installing on another drive, or in a non-default directory.

能够检索已安装的路径也是一个好处,因为现在我当前的代码不考虑安装在另一个驱动器上或非默认目录中的人。

采纳答案by Marcanpilami

The technet script you refer to perfectly works under Win 7 (with Python 2.5 32bits), and I really cannot see why it shouldn't.

您引用的 technet 脚本在 Win 7(使用 Python 2.5 32 位)下完美运行,我真的不明白为什么不应该这样做。

Actually, the real weakness of the WMI approach is that it only lists products installed through the Windows Installer. So it's will not give you the full list. Many programs use different installers. Just compare the results between the (Select * from Win32_Product) and what is displayed in the Control Panel. So, unless you are sure that the program that interset you in your listing are installed with MSI, WMI is definitely not an answer.

实际上,WMI 方法的真正弱点在于它只列出通过 Windows Installer 安装的产品。所以它不会给你完整的列表。许多程序使用不同的安装程序。只需比较 (Select * from Win32_Product) 和控制面板中显示的结果之间的结果。因此,除非您确定在您的列表中插入的程序安装了 MSI,否则 WMI 绝对不是答案。

So it may be not very pythonic, but the best way, as far as I know, is to use the registry as you've done. This is actually how the control panel works, so at least Windows considers it to be the most robust way to do it.

所以它可能不是很pythonic,但据我所知,最好的方法是像你所做的那样使用注册表。这实际上是控制面板的工作方式,因此至少 Windows 认为它​​是最可靠的方式。

回答by DaveSawyer

WMI is the correct way to look for installed programs as it will work across different versions of the OS and will be supported going forward. Looking for specific regkeys may work fine for specific versions of Windows but is not guaranteed to work in the future. Here is some simple python code to check for Box Sync which I just tried on Windows 7. Note that not all fields will be available for every product so be aware these will be 'None.'

WMI 是查找已安装程序的正确方法,因为它可以在不同版本的操作系统上运行,并且会在未来得到支持。查找特定的 regkeys 可能适用于特定版本的 Windows,但不能保证在未来工作。这是一些简单的 Python 代码,用于检查我刚刚在 Windows 7 上尝试过的 Box Sync。请注意,并非所有字段都可用于每个产品,因此请注意这些字段为“无”。

import wmi
w = wmi.WMI()
for p in w.Win32_Product():
    if 'Box, Inc.' == p.Vendor and p.Caption and 'Box Sync' in p.Caption:
        print 'Installed {}'.format(p.Version)

The downside I have seen with WMI is it is very slow to start up.

我在 WMI 中看到的缺点是启动速度非常慢。