rpy2 import is not working

uday picture uday · Feb 17, 2014 · Viewed 29.3k times · Source

I get an error when I try to import rpy2. Here is the code and error.

>>> import pandas.rpy.common
Traceback (most recent call last):  

File "C:\PF\WinPython-64bit-3.3.3.3\python-3.3.3.amd64\lib\site-packages\IPython\core\interactiveshell.py", line 2828, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

File "<ipython-input-148-c258a0f70d44>", line 1, in <module>
import pandas.rpy.common

File "C:\PF\WinPython-64bit-3.3.3.3\python-3.3.3.amd64\lib\site-packages\pandas\rpy\common.py", line 14, in <module>
from rpy2.robjects.packages import importr

ImportError: No module named 'rpy2'

What could be the issue? I'm using python version 3.3.3 and pandas version 0.13.1

EDIT

Tried to install rpy2 separately.

  1. Directly using python setup.py install gave me an error that os doesn't have a module popen3.

  2. Directly installing the exe (rpy2-2.3.9.win32-py3.3.exe) from Christoph Gohlke's site http://www.lfd.uci.edu/~gohlke/pythonlibs/ run fine. But if I try to do import pandas.rpy.common as com then I get the following error (issue with the loading the DLL at from rpy2.rinterface._rinterface import * :

    Traceback (most recent call last):
      File "C:\PF\WinPython-64bit-3.3.3.3\python-3.3.3.amd64\lib\site-packages\IPython\core\interactiveshell.py", line 2828, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
    
      File "<ipython-input-10-63ebebefea80>", line 1, in <module>
    import pandas.rpy.common as com
    
      File "C:\PF\WinPython-64bit-3.3.3.3\python-3.3.3.amd64\lib\site-    packages\pandas\rpy\common.py", line 14, in <module>
    
    from rpy2.robjects.packages import importr
    
      File "C:\PF\WinPython-64bit-3.3.3.3\python-3.3.3.amd64\lib\site-packages\rpy2\robjects\__init__.py", line 15, in <module>
    
        import rpy2.rinterface as rinterface
      File "C:\PF\WinPython-64bit-3.3.3.3\python-3.3.3.amd64\lib\site-packages\rpy2\rinterface\__init__.py", line 103, in <module>
    
        from rpy2.rinterface._rinterface import *
    ImportError: DLL load failed: %1 is not a valid Win32 application.
    

EDIT

Solved it finally. It seems like adding R_HOME and R_USER environment variables did the trick.

Answer

mwaskom picture mwaskom · Feb 17, 2014

RPy2 is a separate project from Pandas (homepage here), so you'll have to install it separately. You should be able to do pip install rpy2, then restart Python and try again.