By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,814 Members | 1,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,814 IT Pros & Developers. It's quick & easy.

Re: subprocess with shared environment?

P: n/a
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"})
Nov 17 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.