Set Windows Environment Variable

Discussion in 'Python' started by Fuzzyman, Mar 30, 2006.

  1. Fuzzyman

    Fuzzyman Guest

    Hello all,

    I would like to set a Windows Environment variable for another
    (non-child) process.

    This means that the following *doesn't* work : ::

    os.environ['NAME'] = value

    In the ``win32api`` package there is a ``GetEnvironmentVariable``
    function, but no ``SetEnvironmentVariable``. Any options ?

    Fuzzyman
    http://www.voidspace.org.uk/python/index.shtml
    Fuzzyman, Mar 30, 2006
    #1
    1. Advertising

  2. Fuzzyman

    Duncan Booth Guest

    Fuzzyman wrote:

    > Hello all,
    >
    > I would like to set a Windows Environment variable for another
    > (non-child) process.
    >
    > This means that the following *doesn't* work : ::
    >
    > os.environ['NAME'] = value
    >
    > In the ``win32api`` package there is a ``GetEnvironmentVariable``
    > function, but no ``SetEnvironmentVariable``. Any options ?


    No, your only option is to find a solution which doesn't involve changing
    another process's environment.
    Duncan Booth, Mar 30, 2006
    #2
    1. Advertising

  3. Fuzzyman

    Magnus Lycka Guest

    Duncan Booth wrote:
    > Fuzzyman wrote:
    >> In the ``win32api`` package there is a ``GetEnvironmentVariable``
    >> function, but no ``SetEnvironmentVariable``. Any options ?

    >
    > No, your only option is to find a solution which doesn't involve changing
    > another process's environment.


    Surely there must be a way to programatically set up the
    environment variables for not yet started processes? I.e.
    doing the same as when you manually change things in the
    control panel. I'm pretty sure many Windows installers do
    that, and while I suppose this is technically a registry
    manipulation, I suspect there is a more direct API somewhere.
    Magnus Lycka, Mar 30, 2006
    #3
  4. Fuzzyman

    Fuzzyman Guest

    Magnus Lycka wrote:
    > Duncan Booth wrote:
    > > Fuzzyman wrote:
    > >> In the ``win32api`` package there is a ``GetEnvironmentVariable``
    > >> function, but no ``SetEnvironmentVariable``. Any options ?

    > >
    > > No, your only option is to find a solution which doesn't involve changing
    > > another process's environment.

    >
    > Surely there must be a way to programatically set up the
    > environment variables for not yet started processes? I.e.
    > doing the same as when you manually change things in the
    > control panel. I'm pretty sure many Windows installers do
    > that, and while I suppose this is technically a registry
    > manipulation, I suspect there is a more direct API somewhere.


    I *believe* that ``SetEnvironmentVariable`` exists in the underlying
    windows API, but that it isn't wrapped by the win32api extension.

    I may be wrong.

    Fuzzyman
    http://www.voidspace.org.uk/python/index.shtml
    Fuzzyman, Mar 30, 2006
    #4
  5. Fuzzyman

    Duncan Booth Guest

    Fuzzyman wrote:

    >
    > Magnus Lycka wrote:
    >> Surely there must be a way to programatically set up the
    >> environment variables for not yet started processes? I.e.
    >> doing the same as when you manually change things in the
    >> control panel. I'm pretty sure many Windows installers do
    >> that, and while I suppose this is technically a registry
    >> manipulation, I suspect there is a more direct API somewhere.

    >
    > I *believe* that ``SetEnvironmentVariable`` exists in the underlying
    > windows API, but that it isn't wrapped by the win32api extension.
    >
    > I may be wrong.
    >

    No, there is a SetEnvironmentVariable call which sets environment variables
    for the current process. From the documentation:

    "This function has no effect on the system environment variables or the
    environment variables of other processes."

    I don't think it will have much effect on the currently running Python
    process either since the you just access os.environ and that won't see
    changes to the system's idea of the environment.

    Magnus:
    I took the original question as asking about already running processes. If
    the question was about processes which have not yet been started then the
    following may be of use:

    "Calling SetEnvironmentVariable has no effect on the system environment
    variables. The user can add or modify system environment variables using
    the Control Panel. To programmatically add or modify system environment
    variables, add them to the
    HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session
    Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE
    message. This allows applications, such as the shell, to pick up your
    updates. Note that environment variables listed in this key are limited to
    1024 characters."

    This would change the environment for any processes which respond to the
    WM_SETTINGCHANGE message, and consequently any processes which they start.
    Window's explorer will then update its environment so that future processes
    that it starts will pick up the changes. I think that most other
    applications, including window's CMD.EXE shell won't respond to this
    message so you wouldn't see the changes in future processes they start.

    See http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/55993 for how
    to do this from Python.
    Duncan Booth, Mar 30, 2006
    #5
  6. Fuzzyman wrote:

    > I *believe* that ``SetEnvironmentVariable`` exists in the underlying
    > windows API, but that it isn't wrapped by the win32api extension.


    SetEnvironmentVariable does the same thing as assignment to os.environ.

    The only way to set the environment for another process is to create the
    process yourself, and pass in an environment block.

    To modify the system environment, you need to update the registry and
    broadcast a window message; see:

    http://msdn.microsoft.com/library/en-us/dllproc/base/environment_variables.asp

    (that cannot be used to modify the environment for arbitrary processes,
    though. it's up to each process to decide how to handle that message,
    if it cares about window messages at all).

    </F>
    Fredrik Lundh, Mar 30, 2006
    #6
  7. Fuzzyman

    Fuzzyman Guest

    Fredrik Lundh wrote:
    > Fuzzyman wrote:
    >
    > > I *believe* that ``SetEnvironmentVariable`` exists in the underlying
    > > windows API, but that it isn't wrapped by the win32api extension.

    >
    > SetEnvironmentVariable does the same thing as assignment to os.environ.
    >
    > The only way to set the environment for another process is to create the
    > process yourself, and pass in an environment block.
    >
    > To modify the system environment, you need to update the registry and
    > broadcast a window message; see:
    >
    > http://msdn.microsoft.com/library/en-us/dllproc/base/environment_variables.asp
    >
    > (that cannot be used to modify the environment for arbitrary processes,
    > though. it's up to each process to decide how to handle that message,
    > if it cares about window messages at all).
    >


    I think I can use the information posted in this thread to achieve what
    I want. Thanks for your help.

    Fuzzyman
    http://www.voidspace.org.uk/python/index.shtml

    > </F>
    Fuzzyman, Mar 30, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom
    Replies:
    0
    Views:
    443
  2. Christian

    Set an environment variable

    Christian, Oct 20, 2005, in forum: Python
    Replies:
    28
    Views:
    804
    Jorgen Grahn
    Oct 27, 2005
  3. Replies:
    5
    Views:
    653
  4. chandan
    Replies:
    1
    Views:
    371
    Naraendirakumar R.R.
    Jan 8, 2008
  5. bill

    set DOS environment variable

    bill, Oct 2, 2008, in forum: Python
    Replies:
    1
    Views:
    421
    Dan Upton
    Oct 2, 2008
Loading...

Share This Page