Setting Environment Variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Greg Lindstrom

    Setting Environment Variables

    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 greg.lindstrom@ novasyshealth.c om

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


  • Grant Edwards

    #2
    Re: Setting Environment Variables

    On 2004-10-20, Greg Lindstrom <greg.lindstrom @novasyshealth. com> wrote:
    [color=blue]
    > 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[/color]

    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.
    [color=blue]
    > 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?[/color]

    You don't.
    [color=blue]
    > I've tried os.system ('setprod') and os.system('. setprod'), but neither
    > seemed to work.[/color]

    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!

    Comment

    • Peter Hansen

      #3
      Re: Setting Environment Variables

      Greg Lindstrom wrote:[color=blue]
      > 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.[/color]

      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

      Comment

      • Alex Martelli

        #4
        Re: Setting Environment Variables

        Greg Lindstrom <greg.lindstrom @novasyshealth. com> wrote:
        [color=blue]
        > 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.[/color]

        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

        Comment

        Working...