'How do I check if an application is installed in windows using Python

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.



Solution 1:[1]

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

Solution 2:[2]

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]))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 thuhbruhuhh
Solution 2 Scott Paterson