当前位置: 代码迷 >> python >> 在 conda 虚拟环境中将 `rpy2` 与为 `R` 安装的软件包一起使用吗?
  详细解决方案

在 conda 虚拟环境中将 `rpy2` 与为 `R` 安装的软件包一起使用吗?

热度:64   发布时间:2023-06-13 13:53:12.0

我一直在努力保持我的环境非冗余和干净,所以我创建了一个R环境并清除了我计算机上所有其他现有的R

该环境称为r-conda ,它位于:

/Users/jespinoz/anaconda/envs/r-conda/bin/R

我意识到我没有安装rpy2并通过rpy2安装它,它想安装新版本的R和所有我不想要的r-essentials ,因为我已经有了一个完美的 R 环境。

我意识到我可以为R conda环境中关联的Python安装rpy2

source activate r-conda
pip install rpy2
source deactivate

但并不是所有的路径都排成一排

如何让rpy2识别我的r-conda环境中所有与R相关的文件和路径?

当我尝试导入包时,它没有正确找到文件:

os.environ['R_HOME'] = "/Users/jespinoz/anaconda/envs/r-conda/bin/R"
from rpy2.robjects.packages import importr
importr("dynamicTreeCut")

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-29-8b0a88dfe12d> in <module>()
      1 # os.environ['R_HOME'] = '/Users/jespinoz/anaconda/envs/r-conda/bin/'
      2 os.environ['R_HOME'] = "/Users/jespinoz/anaconda/envs/r-conda/bin/R"
----> 3 from rpy2.robjects.packages import importr
      4 importr("dynamicTreeCut")

/Users/jespinoz/anaconda/lib/python3.6/site-packages/rpy2/robjects/__init__.py in <module>()
     14 import itertools
     15 from datetime import datetime
---> 16 import rpy2.rinterface as rinterface
     17 import rpy2.rlike.container as rlc
     18 

/Users/jespinoz/anaconda/lib/python3.6/site-packages/rpy2/rinterface/__init__.py in <module>()
     90 del(os)
     91 
---> 92 from rpy2.rinterface._rinterface import (baseenv,
     93                                          emptyenv,
     94                                          endr,

ImportError: dlopen(/Users/jespinoz/anaconda/lib/python3.6/site-packages/rpy2/rinterface/_rinterface.cpython-36m-darwin.so, 2): Library not loaded: @rpath/R/lib/libR.dylib
  Referenced from: /Users/jespinoz/anaconda/lib/python3.6/site-packages/rpy2/rinterface/_rinterface.cpython-36m-darwin.so
  Reason: image not found

通过将其添加到我的~/.bash_profile来修复该错误,但生成了一个类似的新错误:我试了一下,错误发生了变化:

export LD_LIBRARY_PATH="/Users/jespinoz/anaconda/envs/r-conda/lib/R/lib/:$LD_LIBRARY_PATH" 

>>> from rpy2.robjects.packages import importr
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/jespinoz/anaconda/lib/python3.6/site-packages/rpy2/robjects/__init__.py", line 16, in <module>
    import rpy2.rinterface as rinterface
  File "/Users/jespinoz/anaconda/lib/python3.6/site-packages/rpy2/rinterface/__init__.py", line 92, in <module>
    from rpy2.rinterface._rinterface import (baseenv,
ImportError: dlopen(/Users/jespinoz/anaconda/lib/python3.6/site-packages/rpy2/rinterface/_rinterface.cpython-36m-darwin.so, 2): Library not loaded: libicuuc.54.dylib
  Referenced from: /Users/jespinoz/anaconda/lib/python3.6/site-packages/rpy2/rinterface/_rinterface.cpython-36m-darwin.so
  Reason: image not found

所以我尝试了这个,然后得到了同样的错误:

export LD_LIBRARY_PATH="/Users/jespinoz/anaconda/envs/r-conda/lib/R/lib/:/Users/jespinoz/anaconda/pkgs/icu-54.1-0/lib/:$LD_LIBRARY_PATH"

如果我使用conda install rpy2它想要安装Python=3.5.2即使我的主要 conda 环境的默认版本是Python=3.6 @asmeurer 建议在我的r-conda环境中安装rpy2时指定Python=3.6 ,但现在它看起来像一个包冲突错误:

(r-conda) jespinozlt-osx:~ jespinoz$ conda install rpy2 python=3.6
Fetching package metadata .............
Solving package specifications: .


UnsatisfiableError: The following specifications were found to be in conflict:
  - python 3.6*
  - r-permute
  - rpy2
Use "conda info <package>" to see the dependencies for each package

您应该conda install rpy2而不是 pip 安装它。 另外,保持环境活跃。 您应该 conda 将python和任何要使用的 Python 包安装到同一个 R 环境中,以便一切都在单个环境中完成。

  相关解决方案