How to use os.putenv() ?

Graham Dumpleton Graham.Dumpleton at gmail.com
Wed Aug 29 21:50:41 EDT 2007


On Aug 30, 11:21 am, goo... at tyeon.com wrote:
> >>> import os
>
> >>> os.environ['PATH']
>
> 'C:\\WINNT\\system32;C:\\WINNT;C:\\WINNT\\System32\\Wbem;%C:\\WINNT%\
> \system32;%C:\\WINNT%;%C:\\WINNT%\\System32\\Wbem'
>
> >>> os.putenv('PATH', 'C:\\WINNT\\system32')
>
> >>> os.environ['PATH']
>
> 'C:\\WINNT\\system32;C:\\WINNT;C:\\WINNT\\System32\\Wbem;%C:\\WINNT%\
> \system32;%C:\\WINNT%;%C:\\WINNT%\\System32\\Wbem'
>
>
>
> What am I doing wrong?  How do I change the value of an environment
> variable?

What you are missing is that os.environ is only populated from the
global process environment at process startup.

If you update os.environ the changes will be pushed into the global
process environment as well. But if you use os.putenv() instead,
bypassing os.environ, the changes will not show in os.environ.

To confirm that the global process environment is being updated, use
os.getenv().

Graham




More information about the Python-list mailing list