Conda 在线和离线环境迁移

2025-10-13 11:40:35

在线迁移

导出环境配置

激活指定环境,并导出配置文件

conda activate env_name
conda env export > environment.yaml

恢复环境

environment.yaml复制到另一台联网的机器上创建环境

conda env create -f environment.yaml

离线迁移

导出环境生成配置

激活指定环境,导出环境所安装包的详细信息配置

conda activate env_name
conda list --explicit > explicit.txt

下载 Conda 包缓存

指定一个包缓存目录,将环境的包下载到该位置

# Windows
set CONDA_PKGS_DIRS=C:\\path

# Linux/macOS
export CONDA_PKGS_DIRS=/path

使用explicit.txt下载依赖包到缓存目录

conda create --name temp_download_env --file explicit.txt --download-only

--download-only表示只下载,但不进行安装(即不会创建出一个环境)

下载 pip 包

pip freeze > requirements.txt
pip download -r requirements.txt -d ./requirements

恢复环境

explicit.txtrequirements.txt、包缓存文件夹和 pip 包文件夹复制到另一台离线的机器上创建环境

修改explicit.txt中包的路径,如果为远程地址修改为只保留文件名部分

# This file may be used to create an environment using:
# $ conda create --name <env> --file <this file>
# platform: win-64
@EXPLICIT
https://repo.anaconda.com/pkgs/main/win-64/ca-certificates-2025.9.9-haa95532_0.conda
https://repo.anaconda.com/pkgs/main/noarch/tzdata-2025b-h04d1e81_0.conda
https://repo.anaconda.com/pkgs/main/win-64/ucrt-10.0.22621.0-haa95532_0.conda
https://repo.anaconda.com/pkgs/main/win-64/vc14_runtime-14.44.35208-h4927774_10.conda
https://repo.anaconda.com/pkgs/main/win-64/vc-14.3-h2df5915_10.conda
https://repo.anaconda.com/pkgs/main/win-64/vs2015_runtime-14.44.35208-ha6b5a95_10.conda
https://repo.anaconda.com/pkgs/main/win-64/bzip2-1.0.8-h2bbff1b_6.conda
https://repo.anaconda.com/pkgs/main/win-64/expat-2.7.1-h8ddb27b_0.conda
https://repo.anaconda.com/pkgs/main/win-64/libffi-3.4.4-hd77b12b_1.conda
https://repo.anaconda.com/pkgs/main/win-64/libzlib-1.3.1-h02ab6af_0.conda
https://repo.anaconda.com/pkgs/main/win-64/openssl-3.0.18-h543e019_0.conda
https://repo.anaconda.com/pkgs/main/win-64/sqlite-3.50.2-hda9a48d_1.conda
https://repo.anaconda.com/pkgs/main/win-64/xz-5.6.4-h4754444_1.conda
https://repo.anaconda.com/pkgs/main/win-64/zlib-1.3.1-h02ab6af_0.conda
https://repo.anaconda.com/pkgs/main/win-64/tk-8.6.15-hf199647_0.conda
https://repo.anaconda.com/pkgs/main/win-64/python-3.12.4-h14ffc60_1.conda
https://repo.anaconda.com/pkgs/main/win-64/setuptools-80.9.0-py312haa95532_0.conda
https://repo.anaconda.com/pkgs/main/win-64/wheel-0.45.1-py312haa95532_0.conda
https://repo.anaconda.com/pkgs/main/noarch/pip-25.2-pyhc872135_0.conda

修改为

# This file may be used to create an environment using:
# $ conda create --name <env> --file <this file>
# platform: win-64
@EXPLICIT
ca-certificates-2025.9.9-haa95532_0.conda
tzdata-2025b-h04d1e81_0.conda
ucrt-10.0.22621.0-haa95532_0.conda
vc14_runtime-14.44.35208-h4927774_10.conda
vc-14.3-h2df5915_10.conda
vs2015_runtime-14.44.35208-ha6b5a95_10.conda
bzip2-1.0.8-h2bbff1b_6.conda
expat-2.7.1-h8ddb27b_0.conda
libffi-3.4.4-hd77b12b_1.conda
libzlib-1.3.1-h02ab6af_0.conda
openssl-3.0.18-h543e019_0.conda
sqlite-3.50.2-hda9a48d_1.conda
xz-5.6.4-h4754444_1.conda
zlib-1.3.1-h02ab6af_0.conda
tk-8.6.15-hf199647_0.conda
python-3.12.4-h14ffc60_1.conda
setuptools-80.9.0-py312haa95532_0.conda
wheel-0.45.1-py312haa95532_0.conda
pip-25.2-pyhc872135_0.conda

explicit.txt复制到包缓存文件夹中,切换当前目录为包缓存文件夹,使用explicit.txt创建环境

conda create --name new_env_name --file explicit.txt --offline

--offline表示 Conda 只从本地缓存路径查找和安装包

激活环境,使用requirements.txt安装 pip 包文件夹下所有包

conda activate new_env_name
pip install --no-index --find-links=./requirements -r requirements.txt

--no-index表示忽略包索引,仅查看--find-links提供的 URLs
--find-links在指定 URL 或路径查找存档
-r --requirement从给定的需求文件安装

如果存在部分库只有源码,且在安装过程中缺少依赖编译报错,可以从requirements.txt将这些库提取到另一个文本文件,先安装预编译的.whl文件,再安装需要编译的库