问题描述
我正在尝试将 rpy2 库导入 Jupyter Notebook,但我无法克服此错误.
I am trying to import the rpy2 library into a Jupyter Notebook but I cannot get past this error.
已添加路径C:Program FilesRR-4.0.2inx64".
The PATH 'C:Program FilesRR-4.0.2inx64' has been added.
这是我计算机上安装的唯一 R 版本.我已经完全卸载并重新安装了 R/Rstudio/Anaconda,但没有运气.
This is the only version of R installed on my computer. I have completely uninstalled and reinstalled R/Rstudio/Anaconda with no luck.
这里是完整的错误:
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-7-098f0d39b3a3> in <module>
----> 1 from rpy2.robjects import pandas2ri
C:Anacondalibsite-packages
py2
objects\__init__.py in <module>
14 from functools import partial
15 import types
---> 16 import rpy2.rinterface as rinterface
17 import rpy2.rlike.container as rlc
18
C:Anacondalibsite-packages
py2
interface.py in <module>
4 import typing
5 from typing import Union
----> 6 from rpy2.rinterface_lib import openrlib
7 import rpy2.rinterface_lib._rinterface_capi as _rinterface
8 import rpy2.rinterface_lib.embedded as embedded
C:Anacondalibsite-packages
py2
interface_libopenrlib.py in <module>
42 rlib = _rinterface_cffi.lib
43 else:
---> 44 rlib = _dlopen_rlib(R_HOME)
45
46
C:Anacondalibsite-packages
py2
interface_libopenrlib.py in _dlopen_rlib(r_home)
35 raise ValueError('The library path cannot be None.')
36 else:
---> 37 rlib = ffi.dlopen(lib_path)
38 return rlib
39
OSError: cannot load library 'C:Program FilesRR-4.0.2inx64R.dll': error 0x7e
这是我用来导入 rpy2 库的代码:
Here is the code I run to import rpy2 library:
from rpy2.robjects import r, pandas2ri
推荐答案
1 - Windows + IDE
对于那些不使用 Anaconda 的人,请在 Windows 的环境变量 PATH 中添加以下内容:
1 - Windows + IDE
For those not using Anaconda, add the following in Windows' environment variables PATH:
C:Program FilesRR-4.0.3inx64
您的 R 版本可能与R-4.0.3"不同
Your R version may differ from "R-4.0.3"
否则,请查看 Grayson Felt 的回复:
Otherwise, check Grayson Felt's reply:
添加路径
C:UsersusernameAnaconda2;C:UsersusernameAnaconda2Scripts;C:UsersusernameAnaconda2Libraryin;C:UsersusernameAnaconda2Librarymingw-w64lib;C:UsersusernameAnaconda2Librarymingw-w64in
随后重新启动 Anaconda 解决了该问题.
and subsequently restarting Anaconda fixed the issue.
3 - 代码头 Windows 基础
或者,遵循布鲁诺的建议(并且更加精明):
3 - Code header Windows basic
Alternatively, following Bruno's suggestion (and being more sohpisticated):
try:
import rpy2.robjects as robjects
except OSError as e:
try:
import os
import platform
if ('Windows', 'Microsoft') in platform.system():
os.environ["R_HOME"] = 'C:/Program Files/R/R-4.0.3/bin/x64' # Your R version here 'R-4.0.3'
os.environ["PATH"] = "C:/Program Files/R/R-4.0.3/bin/x64" + ";" + os.environ["PATH"]
import rpy2.robjects as robjects
except OSError:
raise(e)
此代码对非 Windows 平台无效.对于不同的 R 版本,也可能需要进行调整.如果情况比这更复杂,您可能应该选择解决方案 1 或 2.
This code won't be effective for non-Windows platform.Also adjustments may be necessary for different R versions.If it gets more complicated than this, you should probably just go for solutions 1 or 2.
注意:如果您的 Python 和 R 版本采用不同的架构(x86 与 x64),您也可能会遇到此问题
NOTE: You may also face this issue if your Python and R versions are in different architechtures (x86 vs x64)
这篇关于OSError: 无法加载库 'C:Program FilesRR-4.0.2inx64R.dll': 错误 0x7e的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!