surprising behaviour of os.environ.clear

s0suk3 at s0suk3 at
Sat Jun 28 08:54:22 CEST 2008

On Jun 27, 4:05 pm, "Joe P. Cool" <joe.p.c... at> wrote:
> If I call os.environ.clear in a python program child processes still
> see the deleted entries. But when I iterate over the keys like so
> names =  os.environ.keys
> for k in names:
>     del  os.environ[k]
> then the entries are also deleted for the child processes. Where is
> the difference? Is this a bug?
> (Observed in Python 2.5.2)

For one thing, the expression 'os.environ.keys' will yield a method
object (not a list, as you're probably expecting), but iterating over
a method as you did should produce an exception. If you want to get
the list of environment vars, you have to call the method, like

Also, aren't changes to environment vars supposed to be visible to
child processes anyway? Which one are you suggesting that behaves the
wrong way, 'os.environ.clear()' or 'del os.environ[key]'?

More information about the Python-list mailing list