How to capture all the environment variables from shell?

Rhodri James rhodri at wildebst.demon.co.uk
Mon Jul 26 20:02:57 EDT 2010


On Tue, 27 Jul 2010 00:36:12 +0100, Peng Yu <pengyu.ut at gmail.com> wrote:

> R_HOME is set in my shell (bash). But os.environ doesn't have it. I'm
> not sure what it does when os module is imported. But it seems that
> os.environ doesn't capture all the environment variable from the
> shell. Could anybody let me know what is the correct way to inherent
> all the environment variables form the shell?

os.environ does capture all the environment that the shell passes to it.
In this case, you haven't exported R_HOME, so the shell doesn't export
it, so os.environ has no chance to capture it.

rhodri at gnudebst:~$ HELLO=world
rhodri at gnudebst:~$ echo $HELLO
world
rhodri at gnudebst:~$ export HELLO
rhodri at gnudebst:~$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['HELLO']
'world'


-- 
Rhodri James *-* Wildebeest Herder to the Masses



More information about the Python-list mailing list