(name, all = False)
which(name, flags = os.X_OK, all = False) -> str or str set

    Works as the system command ``which``; searches $PATH for ``name`` and
    returns a full path if found.

    If `all` is :const:`True` the set of all found locations is returned, else
    the first occurrence or :const:`None` is returned.

    Arguments:
      `name` (str): The file to search for.
      `all` (bool):  Whether to return all locations where `name` was found.

    Returns:
      If `all` is :const:`True` the set of all locations where `name` was found,
      else the first location or :const:`None` if not found.

    Example:
      >>> which('sh')
      '/bin/sh'
which(name, flags = os.X_OK, all = False) -> str or str set
which ( name flags = os . X_OK all = False ) - > str or str set
def which(name, all = False):
    """which(name, flags = os.X_OK, all = False) -> str or str set

    Works as the system command ``which``; searches $PATH for ``name`` and
    returns a full path if found.

    If `all` is :const:`True` the set of all found locations is returned, else
    the first occurrence or :const:`None` is returned.

    Arguments:
      `name` (str): The file to search for.
      `all` (bool):  Whether to return all locations where `name` was found.

    Returns:
      If `all` is :const:`True` the set of all locations where `name` was found,
      else the first location or :const:`None` if not found.

    Example:
      >>> which('sh')
      '/bin/sh'
    """
    # If name is a path, do not attempt to resolve it.
    if os.path.sep in name:
        return name

    isroot = os.getuid() == 0
    out = set()
    try:
        path = os.environ['PATH']
    except KeyError:
        log.exception('Environment variable $PATH is not set')
    for p in path.split(os.pathsep):
        p = os.path.join(p, name)
        if os.access(p, os.X_OK):
            st = os.stat(p)
            if not stat.S_ISREG(st.st_mode):
                continue
            # work around this issue: https://bugs.python.org/issue9311
            if isroot and not \
              st.st_mode & (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH):
                continue
            if all:
                out.add(p)
            else:
                return p
    if all:
        return out
    else:
        return None
def which ( name , all = False ) : # If name is a path, do not attempt to resolve it. if os . path . sep in name : return name isroot = os . getuid ( ) == 0 out = set ( ) try : path = os . environ [ 'PATH' ] except KeyError : log . exception ( 'Environment variable $PATH is not set' ) for p in path . split ( os . pathsep ) : p = os . path . join ( p , name ) if os . access ( p , os . X_OK ) : st = os . stat ( p ) if not stat . S_ISREG ( st . st_mode ) : continue # work around this issue: https://bugs.python.org/issue9311 if isroot and not st . st_mode & ( stat . S_IXUSR | stat . S_IXGRP | stat . S_IXOTH ) : continue if all : out . add ( p ) else : return p if all : return out else : return None