Display environment variables, then append C:\ to PATH. Example program, uses _winreg.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import _winreg
x=_winreg.ConnectRegistry(None,_winreg.HKEY_LOCAL_MACHINE)
y= _winreg.OpenKey(x,
r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment")
print "Your environment variables are"
print "#","name","value","type"
for i in range(1000):
try:
n,v,t=_winreg.EnumValue(y,i)
print i,n,v,t
except EnvironmentError:
print "You have",i,"Environment variables"
break
print "Your PATH was "
path = _winreg.QueryValueEx(y,"path")[0]
print path
_winreg.CloseKey(y)
# Reopen Environment key for writing.
y=_winreg.OpenKey(x,
r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment",
0,_winreg.KEY_ALL_ACCESS)
# now append C:\ to the path
_winreg.SetValueEx(y,"path",0,_winreg.REG_EXPAND_SZ,path+";C:\\")
_winreg.CloseKey(y)
_winreg.CloseKey(x)
|
Install utilities may have a need for changing the path in the registry. _winreg (from the standard library) is the right tool for this endeavour. According to docs, it is desired to become complemented by a more abstract winreg module, sometime in the future.
This example displays and modifies the _system_ environment variables, not those of the current user.
environment changes do not have an effect immediately. The changes made to the environment in the proposed way do not have an effect immediately, even not for a newly start cmd.exe. The following function refreshEnvironment(), if called after setting the environment in the registry lets newly started programs (cmd.exe) "see" the new environment values.
Thanks for this method to Geoffrey Faivre-Malloy and Ronny Lipshitz. See http://www.installsite.org/files/PathSetup.zip