Setting an environment variable.

Discussion in 'Python' started by Ashton Fagg, Jan 3, 2012.

  1. Ashton Fagg

    Ashton Fagg Guest

    Hi list.

    A bit new to Python so please forgive my potential ignorance.

    I'm working with an embedded machine, which is using a Python script to
    oversee the acquisition of some data. The supervisor script, which is
    run by crontab every 5 minutes, relies on an environment variable to be
    set. I've tried to set the environment variable inside crontab, however
    this doesn't work when the script runs.

    Is there a nice way to do this inside the supervisor script itself?
    Would an os.system("export foo=/bar/foo/bar") at the very beginning of
    the script do what I want? I would just like to check before I make
    changes, as being an embedded machine it's a bit of a pain to update
    things like this...(read only file system)

    Note: This is for Python 2.4. I have no ability to update to anything
    newer as this is what our codebase relies upon.

    Thanks and regards,
    Ashton.

    --
    Ashton Fagg ()
    Web: http://www.fagg.id.au/~ashton/

    Keep calm and call Batman.
    Ashton Fagg, Jan 3, 2012
    #1
    1. Advertising

  2. Ashton Fagg

    Nobody Guest

    On Tue, 03 Jan 2012 15:45:20 +1000, Ashton Fagg wrote:

    > I'm working with an embedded machine, which is using a Python script to
    > oversee the acquisition of some data. The supervisor script, which is
    > run by crontab every 5 minutes, relies on an environment variable to be
    > set. I've tried to set the environment variable inside crontab, however
    > this doesn't work when the script runs.


    Odd.

    > Is there a nice way to do this inside the supervisor script itself?
    > Would an os.system("export foo=/bar/foo/bar") at the very beginning of
    > the script do what I want?


    No. It would set the variable only for the child process created by
    os.system(), which would be pointless.

    To set an environment variable for the current process (and, by default,
    any child processes), modify os.environ, e.g.:

    os.environ['foo'] = '/bar/foo/bar'

    You can set environment variables for specific child processes created via
    subprocess.Popen() using the env= parameter, e.g.:

    env = os.environ.copy()
    env['foo'] = '/bar/foo/bar'
    p = subprocess.Popen(..., env=env)

    If env= isn't used, the child will inherit the parent's environment.
    Nobody, Jan 3, 2012
    #2
    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. Rahul S.
    Replies:
    3
    Views:
    596
    Flash Gordon
    Nov 1, 2004
  2. Replies:
    5
    Views:
    640
  3. Johan Holmberg

    Setting "variable" global variable ?

    Johan Holmberg, Sep 24, 2003, in forum: Ruby
    Replies:
    6
    Views:
    128
    Alan Chen
    Sep 25, 2003
  4. Une bévue
    Replies:
    3
    Views:
    129
    Une bévue
    Mar 10, 2006
  5. Pavel Ledin

    Setting Windows environment variable

    Pavel Ledin, Sep 20, 2006, in forum: Ruby
    Replies:
    2
    Views:
    149
    Pavel Ledin
    Sep 20, 2006
Loading...

Share This Page