How to use os.putenv() ?

G

google

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

Graham Dumpleton

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
 
R

Ryan Ginstrom

On Behalf Of (e-mail address removed)
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

T

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

Dennis Lee Bieber

Can you tell me what I am still missing please?
Probably that you don't want to use os.putenv()...
'E:\\Python24\\;C:\\GNAT\\bin;C:\\bin;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\PROGRA~1\\MySQL\\MySQL
Server 5.0\\bin;C:\\Program Files\\SciTE;C:\\Program
Files\\Java\\jre1.6.0\\bin;C:\\Program
Files\\Java\\jdk1.6.0\\bin;C:\\Program Files\\Common
Files\\Adobe\\AGL;C:\\MSSQL7\\BINN;c:\\PROGRA~1\\sdb\\programs\\bin;c:\\PROGRA~1\\sdb\\programs\\pgm;C:\\Tcl\\bin;C:\\Program
Files\\QuickTime\\QTSystem\\;C:\\Program Files\\Common Files\\Roxio
Shared\\DLLShared\\;C:\\Program Files\\Common Files\\Roxio
Shared\\9.0\\DLLShared\\;e:\\Python24\\Scripts;c:\\Regina'
os.environ["PATH"] = "c:\\windows\\system32"
os.environ["PATH"] 'c:\\windows\\system32'
os.getenv("PATH") 'c:\\windows\\system32'
os.environ["PATH"] ='E:\\Python24\\;C:\\GNAT\\bin;C:\\bin;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\PROGRA~1\\MySQL\\MySQL Server 5.0\\bin;C:\\Program Files\\SciTE;C:\\Program Files\\Java\\jre1.6.0\\bin;C:\\Program Files\\Java\\jdk1.6.0\\bin;C:\\Program Files\\Common Files\\Adobe\\AGL;C:\\MSSQL7\\BINN;c:\\PROGRA~1\\sdb\\programs\\bin;c:\\PROGRA~1\\sdb\\programs\\pgm;C:\\Tcl\\bin;C:\\Program Files\\QuickTime\\QTSystem\\;C:\\Program Files\\Common Files\\Roxio Shared\\DLLShared\\;C:\\Program Files\\Common Files\\Roxio Shared\\9.0\\DLLShared\\;e:\\Python24\\Scripts;c:\\Regina')
os.environ["PATH"] 'c:\\windows\\system32'
os.getenv("PATH") 'c:\\windows\\system32'
os.environ["PATH"] ='E:\\Python24\\;C:\\GNAT\\bin;C:\\bin;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\PROGRA~1\\MySQL\\MySQL Server 5.0\\bin;C:\\Program Files\\SciTE;C:\\Program Files\\Java\\jre1.6.0\\bin;C:\\Program Files\\Java\\jdk1.6.0\\bin;C:\\Program Files\\Common Files\\Adobe\\AGL;C:\\MSSQL7\\BINN;c:\\PROGRA~1\\sdb\\programs\\bin;c:\\PROGRA~1\\sdb\\programs\\pgm;C:\\Tcl\\bin;C:\\Program Files\\QuickTime\\QTSystem\\;C:\\Program Files\\Common Files\\Roxio Shared\\DLLShared\\;C:\\Program Files\\Common Files\\Roxio Shared\\9.0\\DLLShared\\;e:\\Python24\\Scripts;c:\\Regina'
os.environ["PATH"]
'E:\\Python24\\;C:\\GNAT\\bin;C:\\bin;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\PROGRA~1\\MySQL\\MySQL
Server 5.0\\bin;C:\\Program Files\\SciTE;C:\\Program
Files\\Java\\jre1.6.0\\bin;C:\\Program
Files\\Java\\jdk1.6.0\\bin;C:\\Program Files\\Common
Files\\Adobe\\AGL;C:\\MSSQL7\\BINN;c:\\PROGRA~1\\sdb\\programs\\bin;c:\\PROGRA~1\\sdb\\programs\\pgm;C:\\Tcl\\bin;C:\\Program
Files\\QuickTime\\QTSystem\\;C:\\Program Files\\Common Files\\Roxio
Shared\\DLLShared\\;C:\\Program Files\\Common Files\\Roxio
Shared\\9.0\\DLLShared\\;e:\\Python24\\Scripts;c:\\Regina''E:\\Python24\\;C:\\GNAT\\bin;C:\\bin;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\PROGRA~1\\MySQL\\MySQL
Server 5.0\\bin;C:\\Program Files\\SciTE;C:\\Program
Files\\Java\\jre1.6.0\\bin;C:\\Program
Files\\Java\\jdk1.6.0\\bin;C:\\Program Files\\Common
Files\\Adobe\\AGL;C:\\MSSQL7\\BINN;c:\\PROGRA~1\\sdb\\programs\\bin;c:\\PROGRA~1\\sdb\\programs\\pgm;C:\\Tcl\\bin;C:\\Program
Files\\QuickTime\\QTSystem\\;C:\\Program Files\\Common Files\\Roxio
Shared\\DLLShared\\;C:\\Program Files\\Common Files\\Roxio
Shared\\9.0\\DLLShared\\;e:\\Python24\\Scripts;c:\\Regina'
From the documentation:

""""
putenv( varname, value)

Set the environment variable named varname to the string value. Such
changes to the environment affect subprocesses started with os.system(),
popen() or fork() and execv(). Availability: most flavors of Unix,
Windows.
""""
Note the key phrase: "... affect subprocesses started with..."

Changes via os.putenv() are only seen by the subprocesses created
after the change, they do not change the "current environment"
['E:\\Python24\\;C:\\GNAT\\bin;C:\\bin;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\PROGRA~1\\MySQL\\MySQL
Server 5.0\\bin;C:\\Program Files\\SciTE;C:\\Program
Files\\Java\\jre1.6.0\\bin;C:\\Program
Files\\Java\\jdk1.6.0\\bin;C:\\Program Files\\Common
Files\\Adobe\\AGL;C:\\MSSQL7\\BINN;c:\\PROGRA~1\\sdb\\programs\\bin;c:\\PROGRA~1\\sdb\\programs\\pgm;C:\\Tcl\\bin;C:\\Program
Files\\QuickTime\\QTSystem\\;C:\\Program Files\\Common Files\\Roxio
Shared\\DLLShared\\;C:\\Program Files\\Common Files\\Roxio
Shared\\9.0\\DLLShared\\;e:\\Python24\\Scripts;c:\\Regina\n']
o.close()
os.putenv("PATH", "C:\\windows\\system32")
o, i = popen2.popen2("echo %PATH%")
print o.readlines() ['C:\\windows\\system32\n']
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top