Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to use os.putenv() ?

988 views
Skip to first unread message

goo...@tyeon.com

unread,
Aug 29, 2007, 9:21:11 PM8/29/07
to
>>>
>>> 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?

Graham Dumpleton

unread,
Aug 29, 2007, 9:50:41 PM8/29/07
to

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

Ryan Ginstrom

unread,
Aug 29, 2007, 10:00:56 PM8/29/07
to pytho...@python.org
> On Behalf Of goo...@tyeon.com

> What am I doing wrong? How do I change the value of an
> environment variable?

You'll have to go through the Windows registry. Please have a look at the
following recipe:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/55993

I also have my own routines based on that for getting and setting the path:

##################

import _winreg as winreg
import win32gui
import win32con

REG_KEY_PATH = r'SYSTEM\CurrentControlSet\Control\Session
Manager\Environment'

def set_path(pathval):
"""Set the PATH environment variable"""

try:
reg = winreg.ConnectRegistry(None,
win32con.HKEY_LOCAL_MACHINE)
key = winreg.OpenKey(reg,
REG_KEY_PATH,
0,
win32con.KEY_ALL_ACCESS)

winreg.SetValueEx(key,
'path',
0,
win32con.REG_EXPAND_SZ,
pathval)

win32gui.SendMessage(win32con.HWND_BROADCAST,
win32con.WM_SETTINGCHANGE,
0,
'Environment')

finally:
winreg.CloseKey(key)
winreg.CloseKey(reg)

def get_path():
"""Get the PATH environment variable"""
try:
reg = winreg.ConnectRegistry(None,
win32con.HKEY_LOCAL_MACHINE)
key = winreg.OpenKey(reg,
REG_KEY_PATH,
0,
win32con.KEY_ALL_ACCESS)

return winreg.QueryValueEx(key,
'path')[0]

finally:
winreg.CloseKey(key)
winreg.CloseKey(reg)

##################

Regards,
Ryan Ginstrom

T

unread,
Aug 30, 2007, 1:29:24 PM8/30/07
to
On Aug 29, 9:50 pm, Graham Dumpleton <Graham.Dumple...@gmail.com>
wrote:

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')

Message has been deleted

T

unread,
Aug 30, 2007, 5:17:01 PM8/30/07
to
Thank you everyone!

0 new messages