Mailing List Archive

Setting environment variables.
I'm writing my first python program...

The program is intended to give a menu of choices, from which the
CVSROOT environment variable is set (I'm sick of swapping around
manually for sucking down code from different places). I'm wondering how
I would do that.

I'm running GNU/Linux, is there an issue with processes... eg, after
running my python program as a child process, will the changes to the
environment variable affect it's parent (the *term)? (suspecting no) Is
there annother way to set the parent's?
Setting environment variables. [ In reply to ]
Oliver White wrote:
eg, after
> running my python program as a child process, will the changes to the
> environment variable affect it's parent (the *term)? (suspecting no) Is
> there annother way to set the parent's?

I'm wondering, whether one could exec to a program which sets
ones env and then exec back to a shell, which would then have
the desired env. One would no more be in the original shell,
but the new one would have the desired env.
Maybe some guru could elaborate on that....

-Per.
--
Per Kistler kistler@fnmail.com / kistler@gmx.net
------------------------------------------------------------
Setting environment variables. [ In reply to ]
On Fri, Aug 20, 1999 at 01:21:23PM +0200, Per Kistler wrote:

> Oliver White wrote:
> eg, after
> > running my python program as a child process, will the changes to the
> > environment variable affect it's parent (the *term)? (suspecting no) Is
> > there annother way to set the parent's?

> I'm wondering, whether one could exec to a program which sets
> ones env and then exec back to a shell, which would then have
> the desired env. One would no more be in the original shell,
> but the new one would have the desired env.
> Maybe some guru could elaborate on that....

It gets better... C's exec*() family allow you to specify the environment.
And so does python :)

(from the Python Library Reference 6.1.5, os module, Process Management)

execle (path, arg0, arg1, ..., env)
This is equivalent to "execve(path, (arg0, arg1, ...), env)".
Availability: Unix, Windows.

[..]

execve (path, args, env)
Execute the executable path with argument list args, and environment
env, replacing the current process (i.e., the Python interpreter). The
argument list may be a tuple or list of strings. The environment must
be a dictionary mapping strings to strings. Availability: Unix,
Windows

>>> x = UserDict.UserDict(os.environ)
>>> x["spam"] = "eggs"

Just so you know i aint cheating this time ;-)

>>> os.environ["spam"]
Traceback (innermost last):
File "<stdin>", line 1, in ?
File "/usr/lib/python1.5/UserDict.py", line 14, in __getitem__
def __getitem__(self, key): return self.data[key]
KeyError: spam

>>> os.execle("/bin/tcsh", "-", x)
centurion:~/python > echo $spam
eggs

Yes, you could set os.environ before an os.exec*(), but passing a modified
env is nicer.

--
Thomas Wouters <thomas@xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!
Setting environment variables. [ In reply to ]
Oliver White wrote:
>
> I'm writing my first python program...
>
> The program is intended to give a menu of choices, from which the
> CVSROOT environment variable is set (I'm sick of swapping around
> manually for sucking down code from different places). I'm wondering how
> I would do that.
>
> I'm running GNU/Linux, is there an issue with processes... eg, after
> running my python program as a child process, will the changes to the
> environment variable affect it's parent (the *term)? (suspecting no) Is
> there annother way to set the parent's?

You would be correct, changing environment variables in a child process
won't affect the parents environment variables. A way to do it is have
the child process print the new value of the environment variable to
stdout and have the parent read this value (probably using os.popen or
similar) and set its environment accordingly.
--
"Programmers are just machines that convert coffee to executable code"
-Unknown

"Drew Csillag" <drew_csillag@geocities.com>
Setting environment variables. [ In reply to ]
Hi Thomas

I don't know UserDict yet, but it seems to work like that:

#!/per/bin/python
# Change env and mutate back to a shell

import os

shell = "/usr/bin/bash"
os.environ["MYVAR"] = "MYVALUE"
env = {}
for k,v in os.environ.items(): env[k] = v
os.execle( shell, "-", env )

Then I end up in a shell with the new environment:-)

Thanks a lot (, although it was not my original question)!
-Per.

Thomas Wouters wrote:

> >>> x = UserDict.UserDict(os.environ)
> >>> x["spam"] = "eggs"
>
> Just so you know i aint cheating this time ;-)
>
> >>> os.environ["spam"]
> Traceback (innermost last):
> File "<stdin>", line 1, in ?
> File "/usr/lib/python1.5/UserDict.py", line 14, in __getitem__
> def __getitem__(self, key): return self.data[key]
> KeyError: spam
>
> >>> os.execle("/bin/tcsh", "-", x)
> centurion:~/python > echo $spam
> eggs
>
> Yes, you could set os.environ before an os.exec*(), but passing a modified
> env is nicer.
>
> --
> Thomas Wouters <thomas@xs4all.net>
>
> Hi! I'm a .signature virus! copy me into your .signature file to help me spread!

--
Per Kistler kistler@fnmail.com / kistler@gmx.net
------------------------------------------------------------
Setting environment variables. [ In reply to ]
In article <37BDB88C.212EC063@starmedia.net>,
Andrew Csillag <andrew@starmedia.net> wrote:
>Oliver White wrote:
.
.
.
>> I'm running GNU/Linux, is there an issue with processes... eg, after
>> running my python program as a child process, will the changes to the
>> environment variable affect it's parent (the *term)? (suspecting no) Is
>> there annother way to set the parent's?
>
>You would be correct, changing environment variables in a child process
>won't affect the parents environment variables. A way to do it is have
>the child process print the new value of the environment variable to
>stdout and have the parent read this value (probably using os.popen or
>similar) and set its environment accordingly.
.
.
.
What's traditional for Unix programmers working within
{sh,bash,ksh,...} is to
eval `my_env_setting_app`
at the command prompt.
--

Cameron Laird http://starbase.neosoft.com/~claird/home.html
claird@NeoSoft.com +1 281 996 8546 FAX