在 conda 上使用 rpy2 时如何 select R 安装?
How to select R installation when using rpy2 on conda?
tl;博士
我可以配置 rpy2 以便它加载 conda 安装而不是系统安装吗?
我的问题
当我 运行 rpy2
它使用 R 的系统安装,但它从我的 R 的 conda 安装中自省和加载库。
这有各种副作用,例如段错误等...
复制
我在 Ubuntu 18 服务器上安装了 R,这是我的系统管理员安装的。
我想将 conda 与 python 和 rpy2 一起使用,以确保我可以自由地使用 select 版本。
在下面,您可以看到我在一个名为 (rnvp)
的 conda 环境中,rpy2
标识 R 的 4.0.5 conda 安装,但是 rpy2.robjects
库加载了系统 R 安装,即 4.0.3.
当 运行 运行 situation
脚本时,我得到以下内容
(rnvp) ludhu126@hyperion:~/real-nvp/real-nvp$ python
Python 3.9.6 (default, Aug 18 2021, 19:38:01)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import rpy2.robjects as ro
During startup - Warning messages:
1: package ‘methods’ was built under R version 4.0.5
2: package ‘datasets’ was built under R version 4.0.5
3: package ‘utils’ was built under R version 4.0.5
4: package ‘grDevices’ was built under R version 4.0.5
5: package ‘graphics’ was built under R version 4.0.5
6: package ‘stats’ was built under R version 4.0.5
>>> print(ro.r)
<rpy2.robjects.R object at 0x7f8f16ece8b0>
platform: x86_64-pc-linux-gnu
arch: x86_64
os: linux-gnu
system: x86_64, linux-gnu
status:
major: 4
minor: 0.3
year: 2020
month: 10
day: 10
svn rev: 79318
language: R
version.string: R version 4.0.3 (2020-10-10)
nickname: Bunny-Wunnies Freak Out
>>> import rpy2.situation
>>> for row in rpy2.situation.iter_info():
... print(row)
...
rpy2 version:
3.4.5
Python version:
3.9.6 (default, Aug 18 2021, 19:38:01)
[GCC 7.5.0]
Looking for R's HOME:
Environment variable R_HOME: /home/ludhu126/miniconda3/envs/rnvp/lib/R
Calling `R RHOME`: /home/ludhu126/miniconda3/envs/rnvp/lib/R
Environment variable R_LIBS_USER: None
R's additions to LD_LIBRARY_PATH:
R version:
In the PATH: R version 4.0.5 (2021-03-31) -- "Shake and Throw"
Loading R library from rpy2: OK
Additional directories to load R packages from:
None
C extension compilation:
include:
['/home/ludhu126/miniconda3/envs/rnvp/lib/R/include']
libraries:
['R', 'pcre2-8', 'lzma', 'bz2', 'z', 'rt', 'dl', 'm', 'iconv', 'icuuc', 'icui18n']
library_dirs:
['/home/ludhu126/miniconda3/envs/rnvp/lib', '/home/ludhu126/miniconda3/envs/rnvp/lib/R/lib', '/home/ludhu126/miniconda3/envs/rnvp/lib']
extra_compile_args:
[]
extra_link_args:
['-Wl,--export-dynamic', '-fopenmp', '-Wl,-O2', '-Wl,--sort-common', '-Wl,--as-needed', '-Wl,-z,relro', '-Wl,-z,now', '-Wl,--disable-new-dtags', '-Wl,--gc-sections', '-Wl,-rpath,/home/ludhu126/miniconda3/envs/rnvp/lib', '-Wl,-rpath-link,/home/ludhu126/miniconda3/envs/rnvp/lib']
终端指向R的conda安装,确实是4.0.5
(rnvp) ludhu126@hyperion:~/real-nvp/real-nvp$ which R
/home/ludhu126/miniconda3/envs/rnvp/bin/R
(rnvp) ludhu126@hyperion:~/real-nvp/real-nvp$ R --version
R version 4.0.5 (2021-03-31) -- "Shake and Throw"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-conda-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
https://www.gnu.org/licenses/.
在这个环境之外,我有 /usr/lib/R
的 R 4.0.3
(rnvp) ludhu126@hyperion:~/real-nvp/real-nvp$ conda activate base
(base) ludhu126@hyperion:~/real-nvp/real-nvp$ which R
/usr/bin/R
(base) ludhu126@hyperion:~/real-nvp/real-nvp$ R --version
R version 4.0.3 (2020-10-10) -- "Bunny-Wunnies Freak Out"
Copyright (C) 2020 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
https://www.gnu.org/licenses/.
问题
我可以配置 rpy2 以便它加载 conda 安装吗?
关闭重复项
- How to install rpy2 via conda using default R installation想使用本地安装,而不是conda安装。它也适用于 python 版本 2.7
- Install and use RPy2 (using conda) so that it uses default R installation in /usr/lib/R R 提到了其他问题,例如根本没有安装 rpy2,绑定到 lapack dll 的问题,或者没有设置 R_HOME 变量。这些对我来说都不是问题。
- How to point rpy2 to existing R installation? 表示 PATH 用于解析 R 安装。但我上面的
which R
表明这不是问题。
- force rpy2 in conda environment to use the R outside of the conda environment 是关于加载错误的 R 库。但这很好 - 它会根据需要加载 conda R 安装库。
- Getting Segmentation fault Core dumped error while importing robjects from rpy2 建议卸载基础 R 安装。我不是系统管理员,不能那样做。
在本地重新编译 rpy2,使用 linker 标志来说明 link 到哪个 R 库。
受到博客 post https://thomas-cokelaer.info/blog/2012/01/installing-rpy2-with-different-r-version-already-installed/ 的启发,我决定从源代码重新编译 rpy2
,并将 R 路径作为环境变量提供给编译。
(rnvp) ludhu126@hyperion:~/real-nvp/real-nvp$ export LDFLAGS="-Wl,-rpath,/home/ludhu126/miniconda3/envs/rnvp/lib/R/lib"
(rnvp) ludhu126@hyperion:~/real-nvp/real-nvp$ pip install rpy2 --force-reinstall --compile --no-binary rpy2
Collecting rpy2
Using cached rpy2-3.4.5.tar.gz (194 kB)
Collecting cffi>=1.10.0
Using cached cffi-1.14.6-cp39-cp39-manylinux1_x86_64.whl (405 kB)
Collecting jinja2
Using cached Jinja2-3.0.1-py3-none-any.whl (133 kB)
Collecting pytz
Using cached pytz-2021.1-py2.py3-none-any.whl (510 kB)
Collecting tzlocal
Using cached tzlocal-3.0-py3-none-any.whl (16 kB)
Collecting pycparser
Using cached pycparser-2.20-py2.py3-none-any.whl (112 kB)
Collecting MarkupSafe>=2.0
Using cached MarkupSafe-2.0.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (30 kB)
Skipping wheel build for rpy2, due to binaries being disabled for it.
Installing collected packages: pycparser, MarkupSafe, tzlocal, pytz, jinja2, cffi, rpy2
Attempting uninstall: pycparser
Found existing installation: pycparser 2.20
Uninstalling pycparser-2.20:
Successfully uninstalled pycparser-2.20
Attempting uninstall: MarkupSafe
Found existing installation: MarkupSafe 2.0.1
Uninstalling MarkupSafe-2.0.1:
Successfully uninstalled MarkupSafe-2.0.1
Attempting uninstall: tzlocal
Found existing installation: tzlocal 3.0
Uninstalling tzlocal-3.0:
Successfully uninstalled tzlocal-3.0
Attempting uninstall: pytz
Found existing installation: pytz 2021.1
Uninstalling pytz-2021.1:
Successfully uninstalled pytz-2021.1
Attempting uninstall: jinja2
Found existing installation: Jinja2 3.0.1
Uninstalling Jinja2-3.0.1:
Successfully uninstalled Jinja2-3.0.1
Attempting uninstall: cffi
Found existing installation: cffi 1.14.6
Uninstalling cffi-1.14.6:
Successfully uninstalled cffi-1.14.6
Attempting uninstall: rpy2
Found existing installation: rpy2 3.4.5
Uninstalling rpy2-3.4.5:
Successfully uninstalled rpy2-3.4.5
Running setup.py install for rpy2 ... done
Successfully installed MarkupSafe-2.0.1 cffi-1.14.6 jinja2-3.0.1 pycparser-2.20 pytz-2021.1 rpy2-3.4.5 tzlocal-3.0
(rnvp) ludhu126@hyperion:~/real-nvp/real-nvp$ python
Python 3.9.6 (default, Aug 18 2021, 19:38:01)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import rpy2.robjects as ro; print(ro.r('version'))
_
platform x86_64-conda-linux-gnu
arch x86_64
os linux-gnu
system x86_64, linux-gnu
status
major 4
minor 0.5
year 2021
month 03
day 31
svn rev 80133
language R
version.string R version 4.0.5 (2021-03-31)
nickname Shake and Throw
>>>
运行
python -m rpy2.situation
可以说出很多关于 rpy2
如何看待其环境的信息。除非指定 R_HOME
,否则它将从 PATH
中的第一个 R 中选择它需要的内容。当以其他方式指定不匹配的 LD_LIBRARY_PATH
或预加载不匹配的共享 R 库时,可能会出现问题。
tl;博士 我可以配置 rpy2 以便它加载 conda 安装而不是系统安装吗?
我的问题
当我 运行 rpy2
它使用 R 的系统安装,但它从我的 R 的 conda 安装中自省和加载库。
这有各种副作用,例如段错误等...
复制
我在 Ubuntu 18 服务器上安装了 R,这是我的系统管理员安装的。 我想将 conda 与 python 和 rpy2 一起使用,以确保我可以自由地使用 select 版本。
在下面,您可以看到我在一个名为 (rnvp)
的 conda 环境中,rpy2
标识 R 的 4.0.5 conda 安装,但是 rpy2.robjects
库加载了系统 R 安装,即 4.0.3.
当 运行 运行 situation
脚本时,我得到以下内容
(rnvp) ludhu126@hyperion:~/real-nvp/real-nvp$ python
Python 3.9.6 (default, Aug 18 2021, 19:38:01)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import rpy2.robjects as ro
During startup - Warning messages:
1: package ‘methods’ was built under R version 4.0.5
2: package ‘datasets’ was built under R version 4.0.5
3: package ‘utils’ was built under R version 4.0.5
4: package ‘grDevices’ was built under R version 4.0.5
5: package ‘graphics’ was built under R version 4.0.5
6: package ‘stats’ was built under R version 4.0.5
>>> print(ro.r)
<rpy2.robjects.R object at 0x7f8f16ece8b0>
platform: x86_64-pc-linux-gnu
arch: x86_64
os: linux-gnu
system: x86_64, linux-gnu
status:
major: 4
minor: 0.3
year: 2020
month: 10
day: 10
svn rev: 79318
language: R
version.string: R version 4.0.3 (2020-10-10)
nickname: Bunny-Wunnies Freak Out
>>> import rpy2.situation
>>> for row in rpy2.situation.iter_info():
... print(row)
...
rpy2 version:
3.4.5
Python version:
3.9.6 (default, Aug 18 2021, 19:38:01)
[GCC 7.5.0]
Looking for R's HOME:
Environment variable R_HOME: /home/ludhu126/miniconda3/envs/rnvp/lib/R
Calling `R RHOME`: /home/ludhu126/miniconda3/envs/rnvp/lib/R
Environment variable R_LIBS_USER: None
R's additions to LD_LIBRARY_PATH:
R version:
In the PATH: R version 4.0.5 (2021-03-31) -- "Shake and Throw"
Loading R library from rpy2: OK
Additional directories to load R packages from:
None
C extension compilation:
include:
['/home/ludhu126/miniconda3/envs/rnvp/lib/R/include']
libraries:
['R', 'pcre2-8', 'lzma', 'bz2', 'z', 'rt', 'dl', 'm', 'iconv', 'icuuc', 'icui18n']
library_dirs:
['/home/ludhu126/miniconda3/envs/rnvp/lib', '/home/ludhu126/miniconda3/envs/rnvp/lib/R/lib', '/home/ludhu126/miniconda3/envs/rnvp/lib']
extra_compile_args:
[]
extra_link_args:
['-Wl,--export-dynamic', '-fopenmp', '-Wl,-O2', '-Wl,--sort-common', '-Wl,--as-needed', '-Wl,-z,relro', '-Wl,-z,now', '-Wl,--disable-new-dtags', '-Wl,--gc-sections', '-Wl,-rpath,/home/ludhu126/miniconda3/envs/rnvp/lib', '-Wl,-rpath-link,/home/ludhu126/miniconda3/envs/rnvp/lib']
终端指向R的conda安装,确实是4.0.5
(rnvp) ludhu126@hyperion:~/real-nvp/real-nvp$ which R
/home/ludhu126/miniconda3/envs/rnvp/bin/R
(rnvp) ludhu126@hyperion:~/real-nvp/real-nvp$ R --version
R version 4.0.5 (2021-03-31) -- "Shake and Throw"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-conda-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
https://www.gnu.org/licenses/.
在这个环境之外,我有 /usr/lib/R
(rnvp) ludhu126@hyperion:~/real-nvp/real-nvp$ conda activate base
(base) ludhu126@hyperion:~/real-nvp/real-nvp$ which R
/usr/bin/R
(base) ludhu126@hyperion:~/real-nvp/real-nvp$ R --version
R version 4.0.3 (2020-10-10) -- "Bunny-Wunnies Freak Out"
Copyright (C) 2020 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
https://www.gnu.org/licenses/.
问题 我可以配置 rpy2 以便它加载 conda 安装吗?
关闭重复项
- How to install rpy2 via conda using default R installation想使用本地安装,而不是conda安装。它也适用于 python 版本 2.7
- Install and use RPy2 (using conda) so that it uses default R installation in /usr/lib/R R 提到了其他问题,例如根本没有安装 rpy2,绑定到 lapack dll 的问题,或者没有设置 R_HOME 变量。这些对我来说都不是问题。
- How to point rpy2 to existing R installation? 表示 PATH 用于解析 R 安装。但我上面的
which R
表明这不是问题。 - force rpy2 in conda environment to use the R outside of the conda environment 是关于加载错误的 R 库。但这很好 - 它会根据需要加载 conda R 安装库。
- Getting Segmentation fault Core dumped error while importing robjects from rpy2 建议卸载基础 R 安装。我不是系统管理员,不能那样做。
在本地重新编译 rpy2,使用 linker 标志来说明 link 到哪个 R 库。
受到博客 post https://thomas-cokelaer.info/blog/2012/01/installing-rpy2-with-different-r-version-already-installed/ 的启发,我决定从源代码重新编译 rpy2
,并将 R 路径作为环境变量提供给编译。
(rnvp) ludhu126@hyperion:~/real-nvp/real-nvp$ export LDFLAGS="-Wl,-rpath,/home/ludhu126/miniconda3/envs/rnvp/lib/R/lib"
(rnvp) ludhu126@hyperion:~/real-nvp/real-nvp$ pip install rpy2 --force-reinstall --compile --no-binary rpy2
Collecting rpy2
Using cached rpy2-3.4.5.tar.gz (194 kB)
Collecting cffi>=1.10.0
Using cached cffi-1.14.6-cp39-cp39-manylinux1_x86_64.whl (405 kB)
Collecting jinja2
Using cached Jinja2-3.0.1-py3-none-any.whl (133 kB)
Collecting pytz
Using cached pytz-2021.1-py2.py3-none-any.whl (510 kB)
Collecting tzlocal
Using cached tzlocal-3.0-py3-none-any.whl (16 kB)
Collecting pycparser
Using cached pycparser-2.20-py2.py3-none-any.whl (112 kB)
Collecting MarkupSafe>=2.0
Using cached MarkupSafe-2.0.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (30 kB)
Skipping wheel build for rpy2, due to binaries being disabled for it.
Installing collected packages: pycparser, MarkupSafe, tzlocal, pytz, jinja2, cffi, rpy2
Attempting uninstall: pycparser
Found existing installation: pycparser 2.20
Uninstalling pycparser-2.20:
Successfully uninstalled pycparser-2.20
Attempting uninstall: MarkupSafe
Found existing installation: MarkupSafe 2.0.1
Uninstalling MarkupSafe-2.0.1:
Successfully uninstalled MarkupSafe-2.0.1
Attempting uninstall: tzlocal
Found existing installation: tzlocal 3.0
Uninstalling tzlocal-3.0:
Successfully uninstalled tzlocal-3.0
Attempting uninstall: pytz
Found existing installation: pytz 2021.1
Uninstalling pytz-2021.1:
Successfully uninstalled pytz-2021.1
Attempting uninstall: jinja2
Found existing installation: Jinja2 3.0.1
Uninstalling Jinja2-3.0.1:
Successfully uninstalled Jinja2-3.0.1
Attempting uninstall: cffi
Found existing installation: cffi 1.14.6
Uninstalling cffi-1.14.6:
Successfully uninstalled cffi-1.14.6
Attempting uninstall: rpy2
Found existing installation: rpy2 3.4.5
Uninstalling rpy2-3.4.5:
Successfully uninstalled rpy2-3.4.5
Running setup.py install for rpy2 ... done
Successfully installed MarkupSafe-2.0.1 cffi-1.14.6 jinja2-3.0.1 pycparser-2.20 pytz-2021.1 rpy2-3.4.5 tzlocal-3.0
(rnvp) ludhu126@hyperion:~/real-nvp/real-nvp$ python
Python 3.9.6 (default, Aug 18 2021, 19:38:01)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import rpy2.robjects as ro; print(ro.r('version'))
_
platform x86_64-conda-linux-gnu
arch x86_64
os linux-gnu
system x86_64, linux-gnu
status
major 4
minor 0.5
year 2021
month 03
day 31
svn rev 80133
language R
version.string R version 4.0.5 (2021-03-31)
nickname Shake and Throw
>>>
运行
python -m rpy2.situation
可以说出很多关于 rpy2
如何看待其环境的信息。除非指定 R_HOME
,否则它将从 PATH
中的第一个 R 中选择它需要的内容。当以其他方式指定不匹配的 LD_LIBRARY_PATH
或预加载不匹配的共享 R 库时,可能会出现问题。