Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subprocess.call env var

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

like image 803
AliBZ Avatar asked Jul 05 '12 18:07

AliBZ


People also ask

What is subprocess call?

Subprocess call():Subprocess has a method call() which can be used to start a program. The parameter is a list of which the first argument must be the program name. The full definition is: subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False) # Run the command described by args.

Does subprocess Popen block?

Popen is nonblocking. call and check_call are blocking. You can make the Popen instance block by calling its wait or communicate method.

What is the difference between subprocess call and run?

The recommended approach to invoking subprocesses is to use the run() function for all use cases it can handle. For more advanced use cases, the underlying Popen interface can be used directly. subprocess. call() is part of the Older high-level API (Prior to Python 3.5).

How do I use subprocess Popen in Python?

To start a new process, or in other words, a new subprocess in Python, you need to use the Popen function call. It is possible to pass two parameters in the function call. The first parameter is the program you want to start, and the second is the file argument.


1 Answers

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/"},
    )
like image 72
siesta Avatar answered Sep 29 '22 23:09

siesta