SYSTEM and USER environment (was Your confirmation is required to join the Python-list mailing list)

Dave Angel davea at ieee.org
Mon May 4 17:10:11 EDT 2009


Allan Yuan wrote:
> Hi,
> I just wanna know how to set SYSTEM variables and USER variables of windows,
> but got no way.
>
> Firstly I thought "os.environ + os.system" may work well, but found no way
> to let "os.environ" run to retrive USER variables.
>
> Then I tried win32api, finding the GetEnvironmentVariables() mixing SYSTEM
> and USER variables up, and SetEnvironmentVariables() failing to add
> variables.
>
> Could you help me, please?
> Thanks a lot.
>
>   
First, you need to put a meaningful subject line on a query to the 
list.  Hopefully you also did the confirmation, so that you'll actually 
be getting the list emails.

Next, your problem.  SYSTEM and USER variables are a uniquely Windows 
concept, and as far as I know, have no direct counterpart in Python.  
These are really just names used in the Control Panel applet to refer to 
two sections of the registry which are used to define which environment 
variables a task will start with, if the task is started directly from 
Explorer.  Tasks that are started by other tasks (eg. the command line) 
get environment variables as defined by the parent.

So, there are at least three obvious ways a process gets started.  One 
is by explorer, in which case it gets the environment described above.  
Two is by a DOS box, in which case it gets the environment variables 
according to the rules of the CMD shell.  And third is from an arbitrary 
3rd party process, in which case it's up to that developer.

So, which are you trying to change?  I'm suspecting you're interested in 
the Explorer version, for example that launches a program from a 
shortcut on the desktop, or via an association with a particular file 
extension.  i don't know which registry variables are involved, but 
there are two registry keys,
     Try:    hklm/SYSTEm/CurrentControlSet/Control/Session 
Manager/Environment    for the SYSTEM environment variables
               and hkcu/Environment     for the USER environment variables

Experiment first with REGEDIT, then when you get the behavior you want,  
look up module _winreg  (or winreg in Python 3.0) to do it 
programmatically.  Notice that existing DOS boxes won't see these new 
variables, only things launched by Explorer or the equivalent.

It's also possible you just want to set environment variables for a 
particular DOS box session.  In that case, use a .BAT or .CMD file, in 
which a SET statement tells the shell what values to use next time it 
launches.





More information about the Python-list mailing list