Setting Environment Variables

G

Greg Lindstrom

Hello-

I am running python 2.3. on an HP-9000 box running Unix and have a POSIX
script that sets up my production environment. I would like to run the
script from inside a python routine and pick up the environment variables
but am having trouble getting it done. Suppose the script is called
setprod. How can I call this from inside a python script then pick up the
env's later in my program? I've tried os.system ('setprod') and
os.system('. setprod'), but neither seemed to work.

Thanks for your help,
--greg

Greg Lindstrom (501) 975-4859
NovaSys Health (e-mail address removed)

"We are the music makers, and we are the dreamers of dreams" W.W.
 
G

Grant Edwards

I am running python 2.3. on an HP-9000 box running Unix and have a POSIX
script that sets up my production environment. I would like to run the
script from inside a python routine and pick up the environment variables

What do you mean by "pick up the environment variables"?

You do not have access to the environment of the child process, if that's
what you mean.
but am having trouble getting it done. Suppose the script is called
setprod. How can I call this from inside a python script then pick up the
env's later in my program?

You don't.
I've tried os.system ('setprod') and os.system('. setprod'), but neither
seemed to work.

That's right.

You're going to have to change the script to print the data of interest to
stdout or stderr, and read it via a pipe. The child process can't change
the environment of the parent, and the parent can't read the environment of
the child.
 
P

Peter Hansen

Greg said:
I am running python 2.3. on an HP-9000 box running Unix and have a POSIX
script that sets up my production environment. I would like to run the
script from inside a python routine and pick up the environment variables
but am having trouble getting it done. Suppose the script is called
setprod. How can I call this from inside a python script then pick up the
env's later in my program? I've tried os.system ('setprod') and
os.system('. setprod'), but neither seemed to work.

I'm not sure of the solution (there are several alternatives)
but the problem is that os.system() creates a new shell (or
subshell, if you will) that is exited when the script completes.
Obviously that means any changes made to the subshell's environment
will not appear in the environment of the process running the
Python script.

The answer depends on what you are trying to accomplish. Do
you need the environment variables to be used only in the Python
script, or are you trying to affect the environment even after
the Python script has completed?

(When people try to do this, it's sort of like trying to affect
the variables in the calling routine from inside a subroutine.
Although that's not usually a good idea, there are (ugly) ways
to work around the roadblocks, such as global variables...
in such a case, and perhaps yours, there may be other ways of
looking at the problem that can inspire more elegant solutions.)

-Peter
 
A

Alex Martelli

Greg Lindstrom said:
but am having trouble getting it done. Suppose the script is called
setprod. How can I call this from inside a python script then pick up the
env's later in my program? I've tried os.system ('setprod') and
os.system('. setprod'), but neither seemed to work.

try parsing the result of: os.popen('. setprod && env').readlines()

the env command emits the current environment to stdout, and the
os.popen() makes that environment available to you. If you want to know
exactly what setprod changed, you can compare the parsed result of that
with the still-current os.environ dictionary: the subprocess (in which
setprod necessarily runs) can't alter YOUR process's environment.


Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Sharing Base Class members 0
Machine identification 1
Pattern Matching 0
mySQL access 2
Boa Constructor Problem 5
Oracle Access via cx_Oracle 0
Oracle Access via cx_Oracle 1
Working with Forms in MS Word 1

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top