How to find the location of an executable in Python?
Asked Answered
F

3

10

What is the best method for identifying an executable within Python?

I found that the following function will find the notepad executable

from shutil import which
which('notepad')
Out[32]: 'C:\\Windows\\system32\\notepad.EXE'

another way to do it.

from distutils import spawn
spawn.find_executable('notepad')
Out[38]: 'C:\\Windows\\system32\\notepad.exe'

While both methods work for notepad, I can't seem to get them to find other executables like vlc.exe, gimp-2.10.exe, or others. What is a better method for finding executable files on a computer?

Furthermore answered 28/3, 2019 at 10:8 Comment(5)
Have a look at this question : https://mcmap.net/q/1166998/-list-of-installed-programsViscous
Both of the methods above will by default search you PATH in order to find the executables. If they are not on the the path it naturally cannot find them. What exactly are you trying to achieve?Monocotyledon
This question was spurred while I was helping answer another question. After doing some searching I found these commands.Furthermore
Your question seems to be very specific to Windows and finding executables which are not necessarily on the PATH. Perhaps edit to clarify the scope?Bingo
I've also seen import sys; sys.executable but not sure if it's a good match for your exact requirement.Ingravescent
S
3

Here is platform independent efficient way to do it:

import subprocess
import os
import platform

def is_tool(name):
    try:
        devnull = open(os.devnull)
        subprocess.Popen([name], stdout=devnull, stderr=devnull).communicate()
    except OSError as e:
        if e.errno == os.errno.ENOENT:
            return False
    return True

def find_prog(prog):
    if is_tool(prog):
        cmd = "where" if platform.system() == "Windows" else "which"
        return subprocess.call([cmd, prog])
Scrutable answered 28/3, 2019 at 10:22 Comment(2)
popen will execute a process which is might be not desired behaviour leading to creation log files, loading dependent libraries and other things.Arvillaarvin
Now errno is individual python module and must independently be imported via import errnoSelden
L
3

I will piggy back on the answer from farshid-ashouri (I don't have enough reputation to comment) to add that in order to return the stdout of the call to which/where we need to use subprocess.check_output, like this:

def find_prog(prog):
    if is_tool(prog):
        cmd = "where" if platform.system() == "Windows" else "which"
        try:
            return subprocess.check_output([cmd, prog])
        except subprocess.CalledProcessError:
            return None

This will return the actual path of the executable and it's probably what you want.

Lysol answered 4/10, 2023 at 8:39 Comment(0)
P
1

Here below is the snippet which would help you to retrieve the necessary details :

Windows Management Instrumentation (WMI) is Microsoft’s implementation of Web-Based Enterprise Management (WBEM), an industry initiative to provide a Common Information Model (CIM) for pretty much any information about a computer system.

import wmi as win_manage

w_instance = win_manage.WMI()
for details in w_instance.Win32_Product():
  print('Name=%s,Publisher=%s,Version=%s,' % (details.Caption, details.Vendor, details.Version))
Pancho answered 28/3, 2019 at 10:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.