可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
Directly using python setup.py install
gave me an error that os
doesn't have a module popen3
.
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.
回答1:
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.
回答2:
This might not apply directly to your question, but ever since pandas and rpy2 have upgraded, their interface has changed. In order to set it up, you must now:
from rpy2.robjects import r, pandas2ri pandas2ri.activate()
Now, in order to change a Pandas dataframe into an R dataframe, one must use pandas2ri.py2ri()
, or use pandas2ri.ri2py()
if you want change an R dataframe into a Pandas dataframe. Further information can be found at https://pandas.pydata.org/pandas-docs/stable/r_interface.html.
回答3:
In my case it worked when I installed it in Anaconda Prompt with a command:
conda install -m rpy2
After that I had to move rpy2 directory from rpy2-2.9.0-py3.6-win32.egg (folder that was installed to) cause it didn't recognized it after installation...and moved only rpy2 folder to the following path:
C:\Users..\Anaconda3\Lib\site-packages (where all packages are called from)
That solved problem for me.
回答4:
That looks like a pandas interface to rpy2. Separately, you also need rpy2 installed on your system. I don't have rpy2, so it was easy for me to check this:
In [1]: import pandas.rpy.common (...) ImportError: No module named rpy2.robjects.packages In [2]: import rpy2 (...) ImportError: No module named rpy2
I installed it using pip:
$ pip install rpy2 In [1]: import pandas.rpy.common
It works! It import rpy2
works for you, but import pandas.rpy.common
does not then the problem might be more subtle.
回答5:
In my case it initially did not wor installing it with conda. I solved it by first changing the active environment.
source activate [environment Name] conda install -c r rpy2=2.8.5
回答6:
I recently ran into a similar issue. I was trying to install rpy2 using anaconda version of python(3.6.3). Below, mentioned command worked for me,
* python3 -m conda install rpy2
回答7:
Well, first you have to install rpy2. If you are using conda you can do it using the following command
conda install -c r rpy2
Then, if you want to use R on Jupyter Notebook environment you can activate R using
%load_ext rpy2.ipython
回答8:
I installed using, conda install -m rpy2
and moved the directory of rpy2 from rpy2-2.9.1-py3.6-win-amd64.egg folder to the path from where the packages are called during the import and then it worked.