在Docker环境下运行R包reticulate

12
在之前的工作中,我使用了“reticulate”包来在R中运行Autogluon自动机器学习库。该代码在我的当前配置(Ubuntu 20.4,R 4.10,reticulate v.125)下运行良好。
然而,在Docker中,这段代码无法运行。

Dockerfile

FROM rocker/r-ver:4.1.0

## Install R packages
RUN R -q -e 'install.packages("remotes")'
RUN R -q -e 'remotes::install_github("rstudio/reticulate")'

# Install Autogluon
RUN R -q -e 'reticulate::install_miniconda()'
RUN R -q -e 'reticulate::conda_create(envname = "r-autogluon", packages = c("python=3.8.13", "numpy"))'
# RUN R -q -e 'reticulate::conda_list()'
RUN R -q -e 'reticulate::conda_install(envname = "r-autogluon", packages = "autogluon", pip = TRUE)'
RUN R -e 'reticulate::use_condaenv("r-autogluon", required = TRUE)'
# RUN -q -e 'reticulate::py_config()'

EXPOSE 3838
CMD R -e 'reticulate::import("autogluon.tabular")'

# Run in shell
# sudo docker build --no-cache -t demo .
# sudo docker run --rm -p 3838:3838 demo

我遇到了这个错误,但不知道该如何解决!

reticulate::import("autogluon.tabular") Error in py_module_import(module, convert = convert) : ModuleNotFoundError: No module named 'autogluon' Calls: -> py_module_import Execution halted

跟踪记录

  • 'conda_list()'指示已成功创建“r-autogluon”!
  • 'py_config()'指示默认使用“r-reticulate”。
  • 'reticulate::use_condaenv("r-autogluon", required = TRUE)'无法工作。

有人有解决方案吗?


我模糊地记得在使用了 use_condaenv 后,reticulate 仍无法正确识别 Python 的版本,而我不得不使用 use_python('/path/to/miniconda/envs/r-autogluon/bin/python') 来指定,尽管这可能应该在 py_config() 中显现出问题。 - PhJ
1个回答

8

RUN R -e 命令执行后关闭会话:

reticulate::import 找不到 autogluon.tabular,是因为 reticulate::use_condaenv('r-autogluon') 在另一个会话中运行,所以 r-autogluon 环境没有在当前会话中设置。

为了在会话启动时运行特定的初始化命令,您需要修改 Rprofile,可以参考 R 启动文件 或者 ?Startup

您可以复制一份经过修改的 Rprofile 文件,或者直接修改该文件:

FROM rocker/r-ver:4.1.0

## Install R packages
RUN R -q -e 'install.packages("remotes")'
RUN R -q -e 'remotes::install_github("rstudio/reticulate")'

# Install Autogluon
RUN R -q -e 'reticulate::install_miniconda()'
RUN R -q -e 'reticulate::conda_create(envname = "r-autogluon", packages = c("python=3.8.13", "numpy"))'
# RUN R -q -e 'reticulate::conda_list()'
RUN R -q -e 'reticulate::conda_install(envname = "r-autogluon", packages = "autogluon", pip = TRUE)'


## Modify Rprofile
RUN R -e 'write("reticulate::use_condaenv(\"r-autogluon\", required = TRUE)",file=file.path(R.home(),"etc","Rprofile.site"),append=TRUE)'
RUN R -e 'write("reticulate::import(\"autogluon.tabular\")",file=file.path(R.home(),"etc","Rprofile.site"),append=TRUE)'

EXPOSE 3838

现在以交互模式运行容器,可以看到 autogluon.tabular 已加载(因为 r-ver docker 文件CMD ["R"] 结尾,所以 R 会自动启动):

docker run --rm -ti demo

R version 4.1.0 (2021-05-18) -- "Camp Pontanezen"
Copyright (C) 2021 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 certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

Module(autogluon.tabular)
>  
 

这是一个非常有见地的答案。不知道是否有一种方法可以编写RUN R -q -e命令以一次执行多行R代码,使用分号使它们在同一个会话中执行。或者运行一个R脚本(作为编辑Rprofile的替代方案)。 - Arthur
你可以将两个R命令用分号;隔开放在一起,例如 RUN R -e 'write("reticulate::use_condaenv(\"r-autogluon\", required = TRUE)",file=file.path(R.home(),"etc","Rprofile.site"),append=TRUE);write("reticulate::import(\"autogluon.tabular\")",file=file.path(R.home(),"etc","Rprofile.site"),append=TRUE)' 可以运行。 - Waldi
您也可以使用source()命令运行任何R脚本。我修改了RProfile以便能够打开交互式会话,但是对于批处理会话来说这并不是必需的。 - Waldi
抱歉,我的问题表述不够清楚。我想问的是,是否可以通过使用分号运行多个命令或者源化一个R脚本来解决这个问题,而无需编辑Rprofile文件。 - Arthur

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接