putenv

Terry Hancock hancock at anansispaceworks.com
Tue Dec 20 07:54:34 CET 2005


On Tue, 20 Dec 2005 05:35:48 -0000
Grant Edwards <grante at visi.com> wrote:
> On 2005-12-20, dpugmire at gmail.com <dpugmire at gmail.com>
> wrote:
> > I have csh script that calls a bunch of python programs
> > and I'd like to use env variables as kind of a global
> > variable that I can pass around to the pythong scripts.
> 
> You can't change the environment of the parent process.
> 
> IOW, the python programs can't change the environment of
> the calling csh script (or, by extenstion, the environment
> of subsequent python programs that are called by the csh
> script).

There is an evil trick, however:

Instead of setting the environment directly, have the python
program return csh code to alter the environment the way you
want, then call the python code by "sourcing" its output:

source `my_script.py`

This will cause the csh script to execute the code and set
the variables, which will then be visible to subsequent
Python scripts.

If you don't want your script to alter anything, then you
just return '' and nothing happens, otherwise you return
something like:

"""
setenv VKG1 spam
setenv VKG2 eggs
"""

It's ugly, but it does work -- I have had to use this
before in a production environment.  Well, it's not really
any less advisable than scripting in csh to begin with. ;-)

Cheers,
Terry

-- 
Terry Hancock (hancock at AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com




More information about the Python-list mailing list