如何基于 yml 文件创建新的 conda env,但具有不同的 Python 版本

How to create a new conda env based on a yml file but with different Python version

我有一个带有 python=3.6.0 及其所有依赖项的 conda 环境。现在我想使用这个 yaml 文件创建另一个具有相同依赖关系但 python=3.7.0 的环境,而不需要一个一个地安装正确版本的包。

# Activate old environment
conda activate so
# Save the list of package to a file:
conda list > log
# Extract the package name but not the version or hash
cat log | awk '{print }' > log2
# make the list of packages
tr '\n' ' ' < log2 > log3
# print the list of packages
cat log3

使用记事本将 python 替换为 python==3.7。然后使用编辑后的包列表创建一个新环境。

conda create --name so2
conda activate so2
conda install _libgcc_mutex ... python==3.7 ... zstd

Conda 将尝试安装所有名称相同但版本不同的包。

  1. 导出最小版本的环境:

    conda env export -n old_env --from-history > env.yaml
    
  2. 在 YAML 的 dependencies 列表中应该有一个 python 条目,如果没有,您可以添加一个。编辑它以获得您想要的 Python 版本。

  3. 创建新环境:

    conda env create -n new_env -f env.yaml