如何在 Python 中获取 Windows 中已安装程序的路径 [英] How to get in Python the path to a installed program in windows

查看:89
本文介绍了如何在 Python 中获取 Windows 中已安装程序的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Python 编写一个脚本,它在 Windows 终端中调用 Ghostscript.

我需要获取一个程序在 windows 中的安装路径(例如 Ghostcript)

是否有任何环境变量或任何其他方法(系统注册表)来获取路径?

解决方案(来自@abernert 的回答:)

 导入 winregprogram_to_found = '软件\\GPL Ghostscript'尝试:h_key = winreg.CreateKey(winreg.HKEY_LOCAL_MACHINE, program_to_found)尝试:gs_version = winreg.EnumKey(h_key, 0)h_subkey = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, program_to_found+'\\'+gs_version)gs_dll = (winreg.EnumValue(h_subkey,0))[1]print("Ghostscript %s 安装在:%s" % (gs_version, gs_dll.replace('gsdll32.dll', '')))除了 OSError:print("Ghostscript 安装不正确!!")除了权限错误:print("找不到Ghostsript!!")

这适用于 winXP 和 win7 32 位系统.

解决方案

你所要求的是不可能的一般.Windows 找不到任意安装的程序.但它可能适用于任何特定应用程序,包括 Ghostscript.

通过 .msi 机制安装的程序或与添加/删除程序"中的卸载"机制交互的其他程序,您可以找到相关条目.但是具有自己的自定义安装程序和卸载程序的程序不必这样做.

可以通过这些关联找到添加文件类型关联"的程序(例如,如果您双击一个 .ps 文件,Windows 就知道如何打开它).

当然,许多程序安装了自己的任意注册表项,您可以随时搜索这些.

如果您查看 Ghostscript 安装文档,它会稍微解释一下关于它的作用.我认为简短的版本是:

  • 有一个选项可以将 GS.EXE 所在的目录添加到您的 %PATH% 中——但在您的情况下,显然它不在那里.
  • 有一个选项可以将 GS.EXE 的路径注册为至少 .ps 文件的文件类型关联,除非其他人已经拥有它.
  • GSDLL32.DLL 的路径可以在 GS_DLL 环境变量中找到,也可以在 HKCU\Software\GPL Ghostscript\#.## 中找到code> 或 HKLM\Software\GPL Ghostscript\#.##(其中 #.## 是主要和次要版本号).当然,不能保证 DLL 和 EXE 位于同一位置(这就是它首先执行所有复杂操作的原因).
  • 卸载程序的路径是通过 Windows 卸载程序机制注册的.当然,不能保证 GS.EXE 与卸载程序在同一目录中.

因为几乎所有这些都是可选的,所以归结为你想投入多少努力来尝试所有不同的可能性.

要从 Python 访问这些注册表项,请参阅 _winreg 标准库中的模块.

I'm writing a scritp in Python that calls Ghostscript in windows terminal.

I need to get the path where a program is installed in windows (e.g. Ghostcript)

There are any environment variable or any other method (system registry) to get the path?

SOLUTION (from @abarnert answer:)

    import winreg

    program_to_found = 'Software\\GPL Ghostscript'

    try:
        h_key = winreg.CreateKey(winreg.HKEY_LOCAL_MACHINE, program_to_found)
        try:
            gs_version = winreg.EnumKey(h_key, 0)
            h_subkey = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, program_to_found+'\\'+gs_version)
            gs_dll = (winreg.EnumValue(h_subkey,0))[1]
            print("Ghostscript %s is installed in: %s" % (gs_version, gs_dll.replace('gsdll32.dll', '')))
        except OSError:
            print("Ghostscript insn't correctly installed!! ")
    except PermissionError:
        print("Ghostsript not found!! ")

This works for winXP and win7 32bit system.

解决方案

What you're asking for is impossible in general. Windows can't find arbitrary installed programs. But it may be possible for any particular app, Ghostscript included.

Programs that were installed by the .msi mechanism or something else that interacts with the "uninstall" mechanism in Add/Remove Programs, you can find entries for that. But programs with their own custom installers and uninstallers don't have to do this.

Programs that add a "file type associations" (so that, e.g., if you double-click a .ps file Windows knows how to open it) can be found through those associations.

And of course many programs install their own arbitrary registry keys, and you can always search for those.

If you look at the Ghostscript installation docs, it explains a little bit about what it does. I think the short version is:

  • There's an option to add the directory that GS.EXE sits in to your %PATH%—but in your case, obviously, it isn't there.
  • There's an option to register the path to GS.EXE as a file type association for at least .ps files, unless something else already owned it.
  • The path to GSDLL32.DLL may be found in the GS_DLL environment variable, or in HKCU\Software\GPL Ghostscript\#.## or HKLM\Software\GPL Ghostscript\#.## (where that #.## is the major and minor version number). Of course there's no guarantee that the DLL and the EXE are in the same location (which is why it does all that complicated stuff in the first place).
  • The path to the uninstaller is registered with the Windows uninstaller mechanism. Of course there's no guarantee that GS.EXE is in the same directory as the uninstaller.

Since almost all of these are optional, it comes down to how much effort you want to put into trying all the different possibilities.

To access these registry keys from Python, see the _winreg module in the stdlib.

这篇关于如何在 Python 中获取 Windows 中已安装程序的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆