Set Windows Environment Variable

F

Fuzzyman

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
 
D

Duncan Booth

Fuzzyman said:
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.
 
M

Magnus Lycka

Duncan said:
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.
 
F

Fuzzyman

Magnus said:
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
 
D

Duncan Booth

Fuzzyman said:
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.
 
F

Fredrik Lundh

Fuzzyman said:
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>
 
F

Fuzzyman

Fredrik said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top