473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Set Windows Environment Variable

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
6 3777
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
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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: sebastien.hugues | last post by:
Hi I would like to retrieve the application data directory path of the logged user on windows XP. To achieve this goal i use the environment variable APPDATA. The logged user has this name:...
4
by: Chuck Anderson | last post by:
I use apache, Php and MySql on my Windows XP machine at home. I have been doing so successfully now for a long time. However, I am now trying to use fopen to open secure URLs (https) and having...
9
by: Eugene | last post by:
I am trying to use db2batch from my XP client against a remote database. And here's what I am getting: C:\work\Tuning\DB2\db2batch> db2batch -d oakl0 -a fs/fs -f db2batch.sql Running in...
2
by: Andrej Litowka | last post by:
Hi All! If anybody know, how I cann set an enviroment variable (with ANSI C) on Windows similar like on Linux. I tried setenv, but the function doesn't work correct on Windows - it sets env....
3
by: Chris Paul | last post by:
I'm having trouble with PHP & PostgreSQL/OpenLDAP/Apache on Windows. I've set this up countless times on BSD (piece of cake) but I'm trying to do this on Windows now so that my developer can work...
2
by: fdmaxey | last post by:
I've written code in VB.Net that calls GetEnvironmentVariable. When I execuate the code as part of a Windows Form, it reads the environment variable with no problem. I put the code in a Windows...
4
by: reachsamdurai | last post by:
Hello, Can you please let me know the procedure to reach db2 command prompt from a cygwin window (hence without using Start-> Run -> db2cmd method). I'm planning to write shell scripts which...
1
by: patrickdub | last post by:
Hi, I'm wondering what kind of things do I need to look out for when converting my .cmg scripts to Unix scripts. (I want the same inputs and outputs, but I'm now running on a Unix Server. I know...
2
by: Ran Raj | last post by:
In Windows environment, I want to create/update both system and user environment variables. By default, I can access/modify the variables using Start > Control panel > System > Advanced >...
4
by: vkbishnoi | last post by:
I need to create an environment variable for another local user in windows. For example, say suppose there are 2 users in the system (User1 and User2). Currently User1 is logged in the system. Now Is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.