Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #18376 > unrolled thread

Setting an environment variable.

Started byAshton Fagg <ashton@fagg.id.au>
First post2012-01-03 15:45 +1000
Last post2012-01-03 20:29 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Setting an environment variable. Ashton Fagg <ashton@fagg.id.au> - 2012-01-03 15:45 +1000
    Re: Setting an environment variable. Nobody <nobody@nowhere.com> - 2012-01-03 20:29 +0000

#18376 — Setting an environment variable.

FromAshton Fagg <ashton@fagg.id.au>
Date2012-01-03 15:45 +1000
SubjectSetting an environment variable.
Message-ID<mailman.4333.1325569526.27778.python-list@python.org>
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 (ashton@fagg.id.au)
Web: http://www.fagg.id.au/~ashton/

Keep calm and call Batman.

[toc] | [next] | [standalone]


#18448

FromNobody <nobody@nowhere.com>
Date2012-01-03 20:29 +0000
Message-ID<pan.2012.01.03.20.29.29.503000@nowhere.com>
In reply to#18376
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web