'sourcing' Python scripts?

Oleg Broytmann phd at sun.med.ru
Wed Apr 28 11:07:44 EDT 1999


Hello!

On Wed, 28 Apr 1999, Haimo G. Zobernig wrote:
> this might be a unix rather than a Python problem, but I need to set

   Yes, it is of unix...

> environment variables from a Python program *in the parent process* 
> that is running the Python script ( a la os.environ['BAR'] = 'foo' ).
> In other words, I want to achieve the equivalent of 'sourcing' the 
> Python script. Can this be done? Even better would be a solution that
> also works on the various WinAbominations... (well, NT at least)

   Don't know about NT, sorry. Will talk about unix.

   Preface.
   When you run a program (python script, e.g.), shell (that run the
program) forks and looses any connection to running program. The program
may do anything it wants (change current directory, for example), but all
chnages are local to the program's process (and children). Parent shell
does not know anything. This is the nature of multitasking environment.
   For the shell, the only way to set environment (change directory, etc.)
is to give commands to that shell.

   Well, now you want to run a program, and pass the results to a parent
shell. Here is how you should do it.
   First, your program should write the shell commands to stdout. Second,
you need to ask the shell to process these commands:
   (in shell command line) eval `myscript.py`
Note "eval" and backticks. Backticks in most (if not all) unix shells mean
"run the command and save its stdout". Eval processes saved stdout as
shell commnads.

   Please note. There are two different kinds of shell syntax - Bourne
shell and C shell.
   Usually any program that intended to run under such conditions, can
output two different set of commands - for Bourne shell and for C shell.
Some programs parse SHELL env var (if it matches /bin/*csh - it is a
C-shell variant, else it is Bourne shell), some programs can be controlled
with command line switch (-c for c-shell, nothing for bourne shell). Choose
your method, but please do not force your users to switch a shell just
because your program can only work with one shell but not another.

   Example:

   (in python program my_prog.py):

if under_cshell: # expect C shell
   print "setenv MY_VAR1 value1"
   print "setenv MY_VAR2 value2"
else: # expect Bourne shell
   print "MY_VAR1=value1"
   print "MY_VAR2=value2"
   print "export MY_VAR1 MY_VAR2"

   (in shell):
eval `my_prog.py`

> Haimo G. Zobernig
> Haimo.Zobernig at cern.ch

Oleg.
---- 
    Oleg Broytmann  National Research Surgery Centre  http://sun.med.ru/~phd/
           Programmers don't die, they just GOSUB without RETURN.





More information about the Python-list mailing list