Python rpy2 导入不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/21838773/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
rpy2 import is not working
提问by uday
I get an error when I try to import rpy2. Here is the code and error.
当我尝试导入 rpy2 时出现错误。这是代码和错误。
>>> 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
可能是什么问题?我正在使用 python 版本 3.3.3 和 Pandas 版本 0.13.1
EDIT
编辑
Tried to install rpy2 separately.
尝试单独安装 rpy2。
- Directly using - python setup.py installgave me an error that- osdoesn'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 comthen 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.
- 直接使用 - python setup.py install给了我一个- os没有模块的错误- popen3。
- 从 Christoph Gohlke 的网站http://www.lfd.uci.edu/~gohlke/pythonlibs/直接安装 exe (rpy2-2.3.9.win32-py3.3.exe)运行良好。但是,如果我尝试这样做, - import pandas.rpy.common as com则会出现以下错误(在以下位置加载 DLL 时出现问题- 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.
终于解决了。似乎添加 R_HOME 和 R_USER 环境变量可以解决问题。
回答by mwaskom
回答by Dan Allan
That looks like a pandas interfaceto rpy2. Separately, you also need rpy2 installed on your system. I don't have rpy2, so it was easy for me to check this:
这看起来像rpy2的 pandas接口。另外,您还需要在系统上安装 rpy2。我没有 rpy2,所以我很容易检查这个:
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 安装它:
$ pip install rpy2
In [1]: import pandas.rpy.common
It works! It import rpy2works for you, but import pandas.rpy.commondoes notthen the problem might be more subtle.
有用!它import rpy2适用于您,但import pandas.rpy.common不适用,那么问题可能会更加微妙。
回答by Benedikt
In my case it initially did not wor installing it with conda. I solved it by first changing the active environment.
在我的情况下,它最初没有用 conda 安装它。我通过首先改变活动环境来解决它。
source activate [environment Name]
conda install -c r rpy2=2.8.5
回答by Bob McBobson
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:
这可能并不直接适用于您的问题,但是自从 pandas 和 rpy2 升级后,它们的界面就发生了变化。为了设置它,您现在必须:
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. 
现在,为了将 Pandas 数据帧更改为 R 数据帧,必须使用pandas2ri.py2ri(),或者pandas2ri.ri2py()如果要将 R 数据帧更改为 Pandas 数据帧,请使用。更多信息可以在https://pandas.pydata.org/pandas-docs/stable/r_interface.html找到。
回答by Pramit
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,
我最近遇到了类似的问题。我试图使用 anaconda 版本的 python( 3.6.3)安装 rpy2 。下面,提到的命令对我有用,
* python3 -m conda install rpy2
回答by Duca
In my case it worked when I installed it in Anaconda Prompt with a command:
就我而言,当我使用以下命令将其安装在 Anaconda Prompt 中时,它起作用了:
conda install -m rpy2
conda install -m rpy2
After that I had to move rpy2directory 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 rpy2folder to the following path:
之后,我不得不将rpy2目录从 rpy2-2.9.0-py3.6-win32.egg(安装到的文件夹)中移动,因为它在安装后无法识别......并且只将rpy2文件夹移动到以下路径:
C:\Users..\Anaconda3\Lib\site-packages (where all packages are called from)
C:\Users..\Anaconda3\Lib\site-packages(所有包都从这里调用)
That solved problem for me.
那为我解决了问题。
回答by ewalel
Well, first you have to install rpy2. If you are using conda you can do it using the following command
好吧,首先你必须安装 rpy2。如果您使用的是 conda,则可以使用以下命令进行操作
conda install -c r rpy2 
Then, if you want to use R on Jupyter Notebook environment you can activate R using
然后,如果您想在 Jupyter Notebook 环境中使用 R,您可以使用
%load_ext rpy2.ipython
回答by AnksG
I installed using, conda install -m rpy2
我使用 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.
并将 rpy2 的目录从 rpy2-2.9.1-py3.6-win-amd64.egg 文件夹移动到导入过程中调用包的路径,然后它就可以工作了。

