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 (eg 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, eg, 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:
GS.EXE
sits in to your %PATH%
—but in your case, obviously, it isn't there.GS.EXE
as a file type association for at least .ps
files, unless something else already owned it.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).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.
You can get path to GhostScript bin folder this way:
from winreg import OpenKey, QueryValue, EnumKey, HKEY_LOCAL_MACHINE, KEY_READ
def get_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'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.