Setting an environment variable.

A

Ashton Fagg

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

Nobody

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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top