Skip to content Skip to sidebar Skip to footer

How To Get In Python The Path To A Installed Program In Windows

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 environm

Solution 1:

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.

Solution 2:

You can get path to GhostScript bin folder this way:

from winreg import OpenKey, QueryValue, EnumKey, HKEY_LOCAL_MACHINE, KEY_READ

defget_ghostscript_path():  # function returns Ghostscript bin folder path
        key = r'SOFTWARE\Artifex\GPL Ghostscript'
        sub_key = OpenKey(HKEY_LOCAL_MACHINE, key, access=KEY_READ)
        return QueryValue(HKEY_LOCAL_MACHINE, f'{key}\\{EnumKey(sub_key, 0)}') + r'\bin'

Post a Comment for "How To Get In Python The Path To A Installed Program In Windows"