Rscript calling rpy2 python script through system() fails

Issue #240 closed
Fabian Hirschmann created an issue

When running a Python script that makes use of rpy2 through Rscript and system(), it fails with the following error:

Traceback (most recent call last):
  File "./test.py", line 2, in <module>
    from rpy2.robjects import r
  File "/usr/local/lib/python2.7/dist-packages/rpy2-2.6.0dev-py2.7-linux-x86_64.egg/rpy2/robjects/__init__.py", line 18, in <module>
    from rpy2.robjects.robject import RObjectMixin, RObject
  File "/usr/local/lib/python2.7/dist-packages/rpy2-2.6.0dev-py2.7-linux-x86_64.egg/rpy2/robjects/robject.py", line 9, in <module>
    class RObjectMixin(object):
  File "/usr/local/lib/python2.7/dist-packages/rpy2-2.6.0dev-py2.7-linux-x86_64.egg/rpy2/robjects/robject.py", line 22, in RObjectMixin
    __show = rpy2.rinterface.baseenv.get("show")
LookupError: 'show' not found

It works flawlessly when executing the python script by itself. This behavior is present in both the version from pypi (2.5.1) and mercurial. All rpy2 tests pass.

Steps to reproduce:

cat << EOF > test.py
#!/usr/bin/env python
from rpy2.robjects import r
EOF

cat << EOF > test.R
#!/usr/bin/env Rscript
system("./test.py")
EOF

chmod +x test.R test.py
./test.R

Comments (2)

  1. Laurent Gautier

    Reinstall rpy2.

    This is almost always caused by a system upgrade or an R upgrade (while the R C-API has changed).

  2. bugmenot

    I think I may have had an issue of the same origin.

    From what I've seen the general consensus is that this LookupError issue is caused by a mismatch between the R version rpy2 was compiled for and the running R version. Whether the version mismatch is the true source of the LookupError issue in most cases, I'm unsure of. However, in this case, if I'm assuming correctly, the actual issue is with environment variables.

    I'm still working on diagnosing my issue, but for me this is what happens: R appears to set some environment variables, and when I run python from it, python will inherit those variables. They seem to be interfering with importing rpy2 in the intended way for some reason.

    My issue may just be caused by the program calling the python code setting the variables incorrectly though. There is more work to be done, I need to be more thorough.

    Your issue may be similar.

    So basically what you might want to do is check for differences between the environment variables of the two python execution contexts. It's what I did.

  3. Log in to comment