import os import shutil import distutils.spawn import subprocess program = 'program' with open(program, "w") as fp: print("#!/usr/bin/bash", file=fp) print('echo "execv() also runs the program"', file=fp) fp.flush() os.chmod(program, 0o777) os.environ['PATH'] = '' print(f"get_exec_path: {os.get_exec_path()!r}") print(f"shutil.which: {shutil.which(program)!r}") print(f"distutils.spawn.find_executable: {distutils.spawn.find_executable(program)!r}") proc = subprocess.run([program], stdout=subprocess.PIPE) print("subprocess runs the program") args = [program] os.execv(args[0], args)