Setting Environment Variables

Discussion in 'Python' started by Greg Lindstrom, Oct 20, 2004.

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

    "We are the music makers, and we are the dreamers of dreams" W.W.
     
    Greg Lindstrom, Oct 20, 2004
    #1
    1. Advertising

  2. On 2004-10-20, Greg Lindstrom <> wrote:

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

    --
    Grant Edwards grante Yow! If I pull this SWITCH
    at I'll be RITA HAYWORTH!! Or
    visi.com a SCIENTOLOGIST!
     
    Grant Edwards, Oct 20, 2004
    #2
    1. Advertising

  3. Greg Lindstrom

    Peter Hansen Guest

    Greg Lindstrom wrote:
    > 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
     
    Peter Hansen, Oct 20, 2004
    #3
  4. Greg Lindstrom <> wrote:

    > 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
     
    Alex Martelli, Oct 20, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick Kasten

    Setting Environment Variables

    Rick Kasten, Jul 7, 2004, in forum: Perl
    Replies:
    2
    Views:
    616
    Howard
    Jul 7, 2004
  2. =?Utf-8?B?YnNmbDQ0?=
    Replies:
    0
    Views:
    506
    =?Utf-8?B?YnNmbDQ0?=
    May 12, 2004
  3. Steve
    Replies:
    6
    Views:
    24,169
    lightsedge
    May 1, 2007
  4. Ulf Meinhardt
    Replies:
    0
    Views:
    578
    Ulf Meinhardt
    Oct 13, 2004
  5. Replies:
    5
    Views:
    700
Loading...

Share This Page