Skip to content

Instantly share code, notes, and snippets.

@simomarsili
Last active September 17, 2020 10:05
Show Gist options
  • Save simomarsili/46a8d50acd1d019ddbb53ba59a1c8d10 to your computer and use it in GitHub Desktop.
Save simomarsili/46a8d50acd1d019ddbb53ba59a1c8d10 to your computer and use it in GitHub Desktop.
which.py
def which(cmd):
"""Path to executable `cmd`.
Args:
cmd (str or list): Name of the executable or list of alternative names.
Returns:
pathlib.Path: Path to executable (the first recoverable path for a list).
"""
try:
from shutil import which as which # python3 only
except ImportError:
from distutils.spawn import find_executable as which
if isinstance(cmd, str):
path = which(cmd)
else:
for c in cmd:
path = which(c)
if path is not None:
break
path = Path(path) if path else None
return path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment