By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,719 Members | 1,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,719 IT Pros & Developers. It's quick & easy.

Set Windows Environment Variable

P: n/a
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

Mar 30 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.
Mar 30 '06 #2

P: n/a
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.
Mar 30 '06 #3

P: n/a

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

Mar 30 '06 #4

P: n/a
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\Contro l\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/Coo...n/Recipe/55993 for how
to do this from Python.
Mar 30 '06 #5

P: n/a
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..._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>

Mar 30 '06 #6

P: n/a

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


Mar 30 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.