Docker镜像中如何让R识别conda环境?GitLab CI部署vetiver遇错
GitLab CI部署Vetiver包时R无法识别Conda环境的问题
我正在通过GitLab CI部署镜像并测试Vetiver包,目前遇到R无法识别conda环境的问题。尝试通过命令echo 'RETICULATE_PYTHON = "/opt/conda/bin' > .Renviron将conda路径写入.Renviron文件,但执行index.qmd时出现以下错误:
Error in normalizePath(conda, winslash = "/", mustWork = TRUE) : path[1]="./conda": No such file or directory
GitLab CI配置 (gitlab-ci.yml)
image: rocker/verse:4.1.2 before_script: - export PATH="/opt/conda/bin:$PATH" - apt-get update --fix-missing && apt-get install -y ca-certificates libglib2.0-0 libxext6 libsm6 libxrender1 libxml2-dev - apt-get install -y python3-pip python3-dev && pip3 install virtualenv - wget --quiet https://repo.anaconda.com/archive/Anaconda3-5.3.1-Linux-x86_64.sh -O ~/anaconda.sh - /bin/bash ~/anaconda.sh -b -p /opt/conda && rm ~/anaconda.sh - ln -s /opt/conda/etc/profile.d/conda.sh /etc/profile.d/conda.sh - echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc - python --version - echo $PATH - echo "source activate base" > ~/.bashrc - echo 'RETICULATE_PYTHON = "/opt/conda/bin' > .Renviron - conda info --envs - R -e "install.packages(c('vetiver','reticulate','tidymodels','pins','dplyr','gt','quarto','checkmate'))"
index.qmd内容(错误始于Python代码块)
format: html: toc: false --- The vetiver framework is for MLOps tasks in Python and R. > *Vetiver, the oil of tranquility, is used as a stabilizing ingredient in perfumery to preserve more volatile fragrances.* The goal of vetiver is to provide fluent tooling to **version**, **deploy**, and **monitor** a trained model. Functions handle both recording and checking the model's input data prototype, and predicting from a remote API endpoint. {fig-align="center" fig-alt="During the MLOps cycle, we collect data, understand and clean the data, train and evaluate a model, deploy the model, and monitor the deployed model. Monitoring can then lead back to collecting more data. There are many great tools available to understand clean data (like pandas and the tidyverse) and to build models (like tidymodels and scikit-learn). Use the vetiver framework to deploy and monitor your models."} ::: callout-tip ## Data scientists have effective tools that they ❤️ to: - collect data - prepare, manipulate, refine data - train models ::: ::: callout-warning ## There is a lack 😩 of effective tools to: - version and publish models - put models into production - monitor model performance ::: Use vetiver to [version](/get-started/version.html) and [deploy](/get-started/deploy.html) your trained models. ::: {.panel-tabset group="language"} ## R ```{r} library(vetiver) cars_lm <- lm(mpg ~ ., data = mtcars) vetiver_model(cars_lm, "cars_linear")
Python
from vetiver import VetiverModel from vetiver.data import mtcars from sklearn import linear_model model = linear_model.LinearRegression().fit(mtcars, mtcars["mpg"]) v = VetiverModel(model, model_name = "cars_linear", save_ptype = True, ptype_data = mtcars) v.description
:::
## 完整错误日志 ```bash $ Rscript -e "quarto::quarto_render('index.qmd', output_file = 'index.html')" processing file: index.qmd |.............. | 20% ordinary text without R code |............................ | 40% label: unnamed-chunk-2 |.......................................... | 60% ordinary text without R code |........................................................ | 80% label: unnamed-chunk-4 (with options) List of 1 $ engine: chr "python" Quitting from lines 46-54 (index.qmd) Error in normalizePath(conda, winslash = "/", mustWork = TRUE) : path[1]="./conda": No such file or directory Calls: .main ... python_munge_path -> get_python_conda_info -> normalizePath Execution halted Error: System command 'quarto' failed, exit status: 1, stdout & stderr were printed Stack trace: 1. quarto::quarto_render("index.qmd", output_file = "index.html") 2. processx::run(quarto_bin, args, echo = TRUE) 3. throw(new_process_error(res, call = sys.call(), echo = echo, ... x System command 'quarto' failed, exit status: 1, stdout & stderr were printed Execution halted Cleaning up project directory and file based variables 00:01 ERROR: Job failed: exit code 1
解决方案建议
1. 修复.Renviron中的Python路径错误
当前写入的路径不完整,缺少python可执行文件名,且等号前后空格不符合R环境变量定义要求,正确命令应为:
echo 'RETICULATE_PYTHON="/opt/conda/bin/python"' > .Renviron
2. 修复.bashrc的覆盖问题
当前用echo "source activate base" > ~/.bashrc覆盖了之前写入的Conda环境加载命令,应改为追加模式:
echo "source activate base" >> ~/.bashrc
3. 直接在CI脚本中激活Conda环境
GitLab CI的脚本为非交互式shell,不会自动加载.bashrc,需在before_script中直接激活:
source /opt/conda/etc/profile.d/conda.sh conda activate base
4. 验证并强制Reticulate使用指定Conda环境
在安装R包后添加验证步骤,确保R能正确连接Conda环境:
R -e "library(reticulate); use_condaenv('/opt/conda', required = TRUE); py_config()"
5. 验证Python环境可用性
在before_script中添加路径验证:
which python python --version
应用以上修改后重新运行CI任务,即可解决R无法识别Conda环境的问题。
内容的提问来源于stack exchange,提问作者brucezepplin
相关产品推荐
相关产品推荐

