无需使用解决依赖关系的工具,您只需安装所有依赖项,然后用于conda env export
生成 pinned/versioned environment.yaml
。
主要缺点:这是较重的重量,因为它实际上必须安装所有依赖项。从好的方面来说,您最终只会得到一个环境“规范”环境文件作为输入,以及一个环境“锁定”文件作为输出。
在 environment-spec.yaml 中指定直接依赖项
同时指定 conda 和 pip 依赖项。例子:
name: base
channels:
- conda-forge
- defaults
# etc.
dependencies:
- matplotlib
- pandas
- pip # needed to have a pip section below
- scikit-learn
- pip:
- pyplot_themes # only available on PyPI
安装依赖项并导出固定版本(包括传递依赖项)
这可以直接在您的本地机器上完成,但这里是如何在 Docker 中隔离此进程:
# syntax=docker/dockerfile:1
# Note: using miniconda instead of micromamba because micromamba lacks the
# `conda env export` command.
FROM continuumio/miniconda3:4.9.2
COPY environment-spec.yml /environment-spec.yml
# mounts are for conda caching and pip caching
RUN --mount=type=cache,target=/opt/conda/pkgs --mount=type=cache,target=/root/.cache \
conda env create -n regen_env --file /environment-spec.yml
# Export dependencies.
RUN conda env export -n regen_env > /environment-lock-raw.yml
CMD ["cat", "/environment-lock.yml"]
然后你可以像这样创建一个固定的环境文件(假设上面的 dockerfile 被命名regen_environment.Dockerfile
):
docker build -t regen_env -f regen_enviroment.Dockerfile .
docker run --rm regen_env > environment-lock.yaml
这会将固定的环境文件输出到environment-lock.yaml
,然后您可以使用conda install -f environment-lock.yaml
.
(这里有一些更多的参考和细节的要点:https ://gist.github.com/jli/b2d2d62ad44b7fcb5101502c08dca1ae )