Setting environment variables.

Thomas Wouters thomas at
Fri Aug 20 17:53:47 CEST 1999

On Fri, Aug 20, 1999 at 01:21:23PM +0200, Per Kistler wrote:

> Oliver White wrote:
>  eg, after
> > running my python program as a child process, will the changes to the
> > environment variable affect it's parent (the *term)? (suspecting no) Is
> > there annother way to set the parent's?

> I'm wondering, whether one could exec to a program which sets
> ones env and then exec back to a shell, which would then have
> the desired env. One would no more be in the original shell,
> but the new one would have the desired env.
> Maybe some guru could elaborate on that....

It gets better... C's exec*() family allow you to specify the environment.
And so does python :)

(from the Python Library Reference 6.1.5, os module, Process Management)

execle (path, arg0, arg1, ..., env)
      This is equivalent to "execve(path, (arg0, arg1, ...), env)".
      Availability: Unix, Windows.


execve (path, args, env) 
      Execute the executable path with argument list args, and environment
      env, replacing the current process (i.e., the Python interpreter). The
      argument list may be a tuple or list of strings. The environment must
      be a dictionary mapping strings to strings. Availability: Unix,

>>> x = UserDict.UserDict(os.environ)
>>> x["spam"] = "eggs"                          

Just so you know i aint cheating this time ;-)

>>> os.environ["spam"]
Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python1.5/", line 14, in __getitem__
    def __getitem__(self, key): return[key]
KeyError: spam

>>> os.execle("/bin/tcsh", "-", x)
centurion:~/python > echo $spam

Yes, you could set os.environ before an os.exec*(), but passing a modified
env is nicer.

Thomas Wouters <thomas at>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!

More information about the Python-list mailing list