Find the path of an executable in Python

A good way to use executables on the system Path, auto-adding the .exe extension on Windows is by Python shutil.which

import shutil
import subprocess

# None if executable not found
exe = shutil.which('ls')

cmd = [exe, '-l']
print(cmd)

subprocess.run(cmd)

The fact that shutil.which() returns None for non-found executables makes it convenient for use with Pytest