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


Groups > comp.lang.python > #18448

Re: Setting an environment variable.

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!tudelft.nl!txtfeed1.tudelft.nl!dedekind.zen.co.uk!zen.net.uk!hamilton.zen.co.uk!shaftesbury.zen.co.uk.POSTED!not-for-mail
From Nobody <nobody@nowhere.com>
Subject Re: Setting an environment variable.
Date Tue, 03 Jan 2012 20:29:29 +0000
User-Agent Pan/0.14.2 (This is not a psychotic episode. It's a cleansing moment of clarity.)
Message-Id <pan.2012.01.03.20.29.29.503000@nowhere.com>
Newsgroups comp.lang.python
References <mailman.4333.1325569526.27778.python-list@python.org>
MIME-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 8bit
Lines 31
Organization Zen Internet
NNTP-Posting-Host 7d4092d7.news.zen.co.uk
X-Trace DXC==3:E``SO7mKb^TX8UA_h:Enok4Z\<mH4IhDU:2=XKPkHKYd23LF23bCQOMCU8L]ShG=VHfneK@URJ
X-Complaints-To abuse@zen.co.uk
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:18448

Show key headers only | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

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

csiph-web