os.putenv ?

Phil Thompson phil at river-bank.demon.co.uk
Wed Mar 26 13:27:55 EST 2003


On Wednesday 26 March 2003 5:43 pm, Chermside, Michael wrote:
> >  > cat > grrr.py
> >
> >  import os
> >  os.putenv("GRRR","This is a late parrot.")
> >  print "GRRR has been set to:", os.getenv("GRRR")
> >
> >  > echo $GRRR
> >  >
> >  > python grrr.py
> >
> >  GRRR has been set to: None
> >
> >  > echo $GRRR
> >
> >  What am I missing ?
>
> While the answer may vary some from OS to OS, in many commonly
> used OSes (eg: unixes) a process may set its OWN environment,
> and it may set the environment of its CHILD processes, but it
> cannot set the environment of its PARENT process. That's why
> in unix "ls" is written as a separate program, but "setenv" is
> built into the shell (if it were a separate program, the
> program could set its own environment, but not that of the
> shell (its parent)).

That explains the result of the echo.

The explanation of the output of the script is (from the docs)...

"When putenv() is supported, assignments to items in os.environ are 
automatically translated into corresponding calls to putenv(); however, calls 
to putenv() don't update os.environ, so it is actually preferable to assign 
to items of os.environ. "

Phil





More information about the Python-list mailing list