How to capture all the environment variables from shell?

Chris Rebert clp2 at rebertia.com
Mon Jul 26 20:02:06 EDT 2010


On Mon, Jul 26, 2010 at 4:36 PM, Peng Yu <pengyu.ut at gmail.com> wrote:
> Hi,
>
> 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?
>
> $ echo $R_HOME
> /opt/R-2.11.1
> $ cat main.py
> #!/usr/bin/env python
>
> import os
>
> print os.environ['R_HOME']
> $ ./main.py
> Traceback (most recent call last):
>  File "./main.py", line 5, in <module>
>    print os.environ['R_HOME']
>  File "/opt/Python-2.6.5/lib/python2.6/UserDict.py", line 22, in __getitem__
>    raise KeyError(key)
> KeyError: 'R_HOME'

You need to "export R_HOME" in bash (probably in your .bashrc or
.bash_profile). See
http://www.ibm.com/developerworks/library/l-bash.html#N10074

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list