subprocess with shared environment?

Albert Hopkins marduk at nbk.hopto.org
Mon Nov 17 18:46:33 EST 2008


On Mon, 2008-11-17 at 15:27 -0800, rowen wrote:
> I'd like to replace some shell scripts with Python, but one step of
> the script modifies my environment in a way that the subsequent steps
> require.
> 
> A simple translation to a few lines of subprocess.call(...) fails
> because the first call modifies the environment, but the other lines
> don't see it.
> 
> Is there a straightforward  way to do this (without having to resort
> to writing some of it as a shell script)?
> 
> -- Russell

>From the subprocess docs

subprocess.call = call(*popenargs, **kwargs)
    Run command with arguments.  Wait for command to complete, then
    return the returncode attribute.
    
    The arguments are the same as for the Popen constructor.  Example:
    
    retcode = call(["ls", "-l"])


...

    class Popen(args, bufsize=0, executable=None,
                stdin=None, stdout=None, stderr=None,
                preexec_fn=None, close_fds=False, shell=False,
                cwd=None, env=None, universal_newlines=False,
                startupinfo=None, creationflags=0):

...

    If env is not None, it defines the environment variables for the new
    process.

...
    Environment example:
    
    os.spawnlpe(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg", env)
    ==>
    Popen(["/bin/mycmd", "myarg"], env={"PATH": "/usr/bin"})





More information about the Python-list mailing list