Modifying the environment passed to subshells was left out of the
interpreter because there seemed to be no well-established portable
way to do it.
However if all you want is to pass environment variables to the
commands run by os.system() or os.popen(), there's a simple solution:
prefix the command string with a couple of variable assignments and
export statements. I guess the following would be universal for popen
(untested):
import os
from commands import mkarg # nifty routine to add shell quoting
def epopen(cmd, mode, env = {}):
# env is a dictionary of environment variables
prefix = ''
for key, value in env.values():
prefix = prefix + '%s=%s\n' % (key, mkarg(value))
prefix = prefix + 'export %s\n' % key
return os.popen(prefix + cmd, mode)
--Guido van Rossum, CWI, Amsterdam <Guido.van.Rossum@cwi.nl>
<URL:http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>