distutils spawn provides the spawn function a frontend to various platform specific functions for launching another program in a subprocess also provides the findexecutable to search the path for a given executable name tries to find executable in the directories listed in path a string listing directories separated by os pathsep defaults to os environ path returns the complete filename or none if not found os confstr or cspath is not available bpo35755 don t use os defpath if the path environment variable is set to an empty string path doesn t match whereas path looks in the current directory the file exists we have a shot at spawn working tries to find executable in the directories listed in path a string listing directories separated by os pathsep defaults to os environ path returns the complete filename or none if not found os confstr or cs_path is not available bpo 35755 don t use os defpath if the path environment variable is set to an empty string path doesn t match whereas path looks in the current directory the file exists we have a shot at spawn working
import sys
import os
import os.path


def find_executable(executable, path=None):
    
    _, ext = os.path.splitext(executable)
    if (sys.platform == 'win32') and (ext != '.exe'):
        executable = executable + '.exe'

    if os.path.isfile(executable):
        return executable

    if path is None:
        path = os.environ.get('PATH', None)
        if path is None:
            try:
                path = os.confstr("CS_PATH")
            except (AttributeError, ValueError):
                
                path = os.defpath
        
        

    
    if not path:
        return None

    paths = path.split(os.pathsep)
    for p in paths:
        f = os.path.join(p, executable)
        if os.path.isfile(f):
            
            return f
    return None