有没有办法找到带有标准库的应用程序的路径?

alw*_*btc 2 python windows installation-path

我想知道是否可以在 Windows 7 下找到具有标准 python 2.7 库的应用程序(例如 MS Excel)的安装目录。我的意思是,它不应该使用任何 pywin32 或 xlrd 等。

也许它会查找注册表来找到安装路径?

Ant*_*ala 5

这可能非常棘手,但是一种方法是在以下位置搜索启动器 exe 位置:HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\foo.exe

因此,像这样的东西(我的这台计算机上没有 Windows,所以如果发现错误,欢迎编辑;),代码应该与 Python 2 和 3 兼容):

try:
    import winreg
except ImportError:
    import _winreg as winreg

handle = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,
    r"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\excel.exe")

num_values = winreg.QueryInfoKey(handle)[1]
for i in range(num_values):
    print(winreg.EnumValue(handle, i))
Run Code Online (Sandbox Code Playgroud)

在 Python 2 上,该模块名为_winreg,但winreg在 Python 3 上。