[Tutor] Re: How to change Windows Environment Variable

dman dsh8290@rit.edu
Sat, 1 Sep 2001 16:26:33 -0400


On Sat, Sep 01, 2001 at 05:02:24PM +0100, alan.gauld@bt.com wrote:
| > > 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!

True, unless for some reason you need to tweak the environment.  Maybe
you are embedding a 3rd party app/lib that checks the environment for
config stuff.

| 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

See os.putenv().

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

Python strings are immutable and the os.environment dictionary is just
a python view of the environment.  Changing may or may not (probably
not) have the desired effect.

HTH,
-D