Reputation: 135
I am using python3.9 with library "pexpect", and I am trying to run some commands in a child process (and then wait for a specific result). The problem is that my child process has different $PATH than the parent, and hence it cannot find some commands that I want to use. My code looks like this:
child = pexpect.spawn(f"foo 1 2", timeout=None)
child.expect(f"foo command ran successfully")
I can run "foo" command in the parent process, but in the child process I get "command not found". Is there a way to solve this? I think I need to make sure the child process has the same $PATH value, but I'm not sure.
Upvotes: 0
Views: 286
Reputation: 83
You can run the shell as interactive and then echo $PATH
. Compare it to another shell instance that is run outside of pexpect and you should be able to see if there's a difference.
Upvotes: 0
Reputation: 135
I think that did the trick:
pexpect.spawn(f"foo 1 2", env=dict(os.environ), timeout=None)
Upvotes: 0
Reputation: 90
You probably want to use env
to set the environment variables in your child process. Something like:
import subprocess
# you could use "foo" instead of "env", but this is more robust
cmd = ["env", "foo", "1", "2"]
subprocess.run(cmd, check=True)
Upvotes: 0