18

I'm using Popen because I need the env, like this:

Popen(
    ["boto-rsync", "..."],
    env={"PATH":"/Library/Frameworks/Python.framework/Versions/2.7/bin/"},
    )

The problem is Popen runs the command as a new thread. Is there any way that I could pass the env to subprocess.call or prevent Popen from creating a new thread? Thanx

Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
AliBZ
  • 4,039
  • 12
  • 45
  • 67
  • 3
    unrelated: `Popen()` does NOT create a new thread (it creates a new *process*). – jfs Jan 29 '16 at 13:28
  • 1
    Just a reminder, `env` replaces the current environment. If you only want to modify it, create a copy first: `new_env = dict(os.environ); new_env['PATH'] = path; Popen(args, env=new_env)` – Flimm Oct 21 '19 at 15:31

1 Answers1

31

You can use env with call in the exact same way as with popen:

subprocess.call(
    ["boto-rsync", "..."],
    env={"PATH":"/Library/Frameworks/Python.framework/Versions/2.7/bin/"},
    )
Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
siesta
  • 1,365
  • 2
  • 16
  • 21
  • Thanx, I don't know why I didn't try it ! I think I couldn't find the option. – AliBZ Jul 05 '12 at 23:55
  • 5
    @AliBZ the documentation for [subprocess.call](https://docs.python.org/2/library/subprocess.html#subprocess.call) is not clear on that point -- the only indication that other keyword args are supported is the tiny little asterisk in `subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)` – Colin D Bennett May 08 '14 at 16:52
  • 3
    @AliBZ: to be clear: `subprocess.call(cmd)` is just `subprocess.Popen(cmd).wait()` i.e., you may pass to `call()` all arguments that you may pass to `Popen()`. – jfs Jan 29 '16 at 13:30