How to use os.putenv() ?

T google at tyeon.com
Thu Aug 30 13:29:24 EDT 2007


On Aug 29, 9:50 pm, Graham Dumpleton <Graham.Dumple... at gmail.com>
wrote:
> 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

Can you tell me what I am still missing please?

>>> import os
>>>
>>> os.getenv('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.getenv('PATH')
'C:\\WINNT\\system32;C:\\WINNT;C:\\WINNT\\System32\\Wbem;%C:\\WINNT%\
\system32;%C:\\WINNT%;%C:\\WINNT%\\System32\\Wbem'
>>>




More information about the Python-list mailing list