From: Peng Yu on
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'

--
Regards,
Peng
From: Chris Rebert on
On Mon, Jul 26, 2010 at 4:36 PM, Peng Yu <pengyu.ut(a)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
From: Rhodri James on
On Tue, 27 Jul 2010 00:36:12 +0100, Peng Yu <pengyu.ut(a)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(a)gnudebst:~$ HELLO=world
rhodri(a)gnudebst:~$ echo $HELLO
world
rhodri(a)gnudebst:~$ export HELLO
rhodri(a)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
From: Cameron Simpson on
On 26Jul2010 18:36, Peng Yu <pengyu.ut(a)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?
|
| $ 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'

Sounds like R_HOME is not exported.

Try these in your shell:

set | grep R_HOME
export | grep R_HOME

Then, presuming it shows only in the first command:

export R_HOME

and then try your python script again.

Cheers,
--
Cameron Simpson <cs(a)zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

It is an approved maxim in war, never to do what the enemy wishes you to
do, for this reason alone, that he desires it. - Napoleon
From: Steven W. Orr on
On 07/26/10 20:02, quoth Chris Rebert:
> On Mon, Jul 26, 2010 at 4:36 PM, Peng Yu <pengyu.ut(a)gmail.com> wrote:
>
> 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

Please! Never export anything from your .bashrc unless you really know what
you're doing. Almost all exports should be done in your .bash_profile

--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net