import os import subprocess program = 'program' with open(program, "w") as fp: print("#!/usr/bin/env bash", file=fp) print('echo "subprocess searchs in the current directory"', file=fp) fp.flush() os.chmod(program, 0o777) args = [program] env = dict(os.environ) env.pop('PATH', None) print("defpath = %s" % os.path.defpath) subprocess.run(args, env=env)