472,146 Members | 1,467 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

How to use os.putenv() ?

>>>
>>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?

Aug 30 '07 #1
4 16187
On Aug 30, 11:21 am, goo...@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

Aug 30 '07 #2
On Behalf Of go****@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/Coo...n/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

Aug 30 '07 #3
T
On Aug 29, 9:50 pm, Graham Dumpleton <Graham.Dumple...@gmail.com>
wrote:
On Aug 30, 11:21 am, goo...@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'
>>>
Aug 30 '07 #4
T
Thank you everyone!

Aug 30 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Warren Oates | last post: by
1 post views Thread by Bathroom_Monkey | last post: by
1 post views Thread by Greg Lindstrom | last post: by
7 posts views Thread by dpugmire | last post: by
2 posts views Thread by LeaFriend | last post: by
4 posts views Thread by Yogi Watcher | last post: by
8 posts views Thread by Michael B Allen | last post: by
4 posts views Thread by ClownPleco | last post: by
reply views Thread by Saiars | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.