[Tutor] Re: How to change Windows Environment Variable

alan.gauld@bt.com alan.gauld@bt.com
Sat, 1 Sep 2001 17:02:24 +0100


> > How to change environment variable on Windows NT or 2000 by python?
> > Private Declare Function SetEnvironmentVariable Lib "kernel32" _
> >     Alias "SetEnvironmentVariableA" (ByVal lpName As String, _
> >     ByVal lpValue As String) As Long

The SetEnvronmentVariable function only changes the value 
*in the current process*. Thus is a fairly pointless function.
You might as well just set a global variable!

This is true in most OSs in that the program gets a copy of 
the environment which it holds in memory, thus changing the 
value only affects the local program. Howeever on Windows 
some of the API functions use env var settings so it does 
make sense(very occasionally)  to change the local copy.

Maybe because its usually a bad idea Python doesn't offer a 
function to do it. But just maybe, the return value of the get
versuion is a pointer to the actual memory space instead of a 
copy - in which case you can try modifying it -  but beware of
overwriting an adjacent value...

In short, be very sure you need to do this. If so then 
proceed with caution. If you really do want it then as 
Ignacio says writing your own as part of win32api might 
be the best route.

Alan G.