4

I am trying to create a program that installs an application on Windows if it is not already installed. I know the file name of the executable but nothing else about it. I want to query the OS to check whether an application of known name or file name is installed on said OS.

All I have so far is the following:

def IsProgramInstalled(ProgramName):    
    """Check whether ProgramName is installed."""

If anyone has the answer to this, it would be much appreciated as I can't find it anywhere.

Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
Chris Stone
  • 129
  • 3
  • 15
  • Try Google search query `site:stackoverflow.com powershell win32_product` this will point you to how this can be achieved using `powershell` (scripting language recommended for Windows administration). Once you know that you can call `powershell` script from your `python` script and post here a [self-answer](http://stackoverflow.com/help/self-answer) – xmojmr Jan 15 '15 at 16:52
  • this question http://stackoverflow.com/questions/429738/detecting-installed-programs-via-registry speaks to where in the registry to find installed programs. You can use the _winreg library in python to query the registry keys. – Wyrmwood Jan 15 '15 at 16:57
  • You can use wmic and parse the CSV result: `cmd = 'wmic.exe product list /format:csv';` `out = subprocess.check_output(cmd, universal_newlines=True).strip();` `products = csv.DictReader(out.splitlines())`. Print the package names (Python 3): `print(*sorted(p['Name'] for p in products), sep='\n')`. – Eryk Sun Jan 16 '15 at 20:57

3 Answers3

5

You can check if a program is installed with shutil:

import shutil

def is_program_installed(program_name):    
    """Check whether program_name is installed."""
    return shutil.which(program_name)

If the program is installed the function will return the path to the program,if the program isn't installed the function will return None.

In my case if I would like to know if git is installed I will get:

git = is_program_installed("git")
print(git)

# Returns: /usr/bin/git

In windows it should return something like: C:\Program Files\Git\bin\git.exe

Daniel Diaz
  • 424
  • 6
  • 12
0

Might be late but i found an answer based on List of installed programs as answers regarding which dont work for user installed programs or programs not in PATH.

import winreg, win32con
def foo(hive, flag):
    aReg = winreg.ConnectRegistry(None, hive)
    aKey = winreg.OpenKey(aReg, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", 0, win32con.KEY_READ | flag)
    count_subkey = winreg.QueryInfoKey(aKey)[0]
    arr = []
    for i in range(count_subkey):
        try:
            asubkey_name = winreg.EnumKey(aKey, i)
            asubkey = winreg.OpenKey(aKey, asubkey_name)
            arr.append([winreg.QueryValueEx(asubkey, "DisplayName")[0], winreg.QueryValueEx(asubkey, "UninstallString")[0]] )
        except EnvironmentError:
            continue
    return arr
x = foo(win32con.HKEY_LOCAL_MACHINE, win32con.KEY_WOW64_32KEY)
y = foo(win32con.HKEY_LOCAL_MACHINE, win32con.KEY_WOW64_64KEY)
z = foo(win32con.HKEY_CURRENT_USER, 0)

That will give you a list of installed programs as a list you can search print([x for x in x+y+z if "My Application Name" in x])

However that does not tell you where it is located.

def bar(hive, flag):
    aReg = winreg.ConnectRegistry(None, hive)
    aKey = winreg.OpenKey(aReg, r'SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders', 0, win32con.KEY_READ | flag)
    count_subkey = winreg.QueryInfoKey(aKey)
    arr = []
    for i in range(count_subkey[1]):
        try:
            arr.append(winreg.EnumValue(aKey, i)[0])
        except EnvironmentError:
            continue
    return arr
w = bar(win32con.HKEY_LOCAL_MACHINE, win32con.KEY_WOW64_64KEY)

That will give you a list of install folders and subfolders which you can easily search through print('\n'.join(x for x in [x for x in w if "My Application Name" in x]))

Scott Paterson
  • 392
  • 2
  • 17
0

As I have tried with a different approach using Python Module winapps. Code snippet at the below.

import winapps

def chk_app_inst(str):
    for item in winapps.list_installed():
        app_name = str
        if app_name in item.name:
            get_path = item.install_location
            get_uninstall_str = item.uninstall_string
            print('App path found:\t', get_path, '\nAlternative Path:\t', get_uninstall_str.replace('uninstall.exe', ''))

So after using this code, at least I can have both ways of getting Installed application's executable file. Kindly let me know if this solution is usable.

Thanks in advance,

Ozzius
  • 131
  • 2
  • 8