使用 conda-pack 迁移 conda 环境
最近可以用学校的服务器来训练模型了。不过学校的服务器是只连了内网的,因此不能使用传统的方法使用 conda。一切资源都必须用 SSH 上传。可以先将要用到的环境打包到本地目录,再将这个目录上传到服务器,最后在服务器上导入环境。
conda-pack
我们将使用 conda-pack 来打包环境。
-
安装
conda-pack:conda install conda-pack -
在本地打包环境:
conda-pack -n my_env # 打包 my_env 环境打包将生成
my_env.tar.gz压缩文件。 -
将生成的压缩文件上传到服务器:
scp my_env.tar.gz USER@HOST: -
在服务器上解包环境:
mkdir -p $CONDA_PREFIX/envs/my_env tar -xzf my_env.tar.gz -C $CONDA_PREFIX/envs/my_env -
激活环境:
conda activate my_env # 激活环境 conda-unpack # 修复可能的路径问题
Troubleshooting
Files managed by conda were found to have been deleted/overwritten
情景:通过 environment.yaml 文件创建环境后,使用 conda-pack 打包环境,提示“由 conda 管理的文件已被删除/改写”,导致无法打包环境:
$ conda-pack -n idm
Collecting packages...
CondaPackError:
Files managed by conda were found to have been deleted/overwritten in the
following packages:
- pillow 11.1.0:
lib/python3.10/site-packages/PIL/_imagingtk.pyi
lib/python3.10/site-packages/pillow-11.1.0.dist-info/INSTALLER
lib/python3.10/site-packages/pillow-11.1.0.dist-info/LICENSE
+ 7 others
This is usually due to `pip` uninstalling or clobbering conda managed files,
resulting in an inconsistent environment. Please check your environment for
conda/pip conflicts using `conda list`, and fix the environment by ensuring
only one version of each package is installed (conda preferred).
这是由于使用 conda 安装的包被 pip 卸载导致的。
建议在通过 environment.yaml 文件创建环境时,仔细检查命令输出,找到哪些包被 pip 卸载:
$ conda env create -f environment.yaml
Channels:
- pytorch
- nvidia
- defaults
- conda-forge
Platform: linux-64
Collecting package metadata (repodata.json): done
Solving environment: done
Downloading and Extracting Packages:
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
Installing pip dependencies: / Ran pip subprocess with arguments:
['/root/.local/opt/miniforge3/envs/idm/bin/python', '-m', 'pip', 'install', '-U', '-r', '/workspace/IDM-VTON/condaenv.m7aj6zqc.requirements.txt', '--exists-action=b']
Pip subprocess output:
Collecting Pillow (from diffusers==0.25.0->-r /workspace/IDM-VTON/condaenv.m7aj6zqc.requirements.txt (line 5))
Using cached pillow-10.4.0-cp310-cp310-manylinux_2_28_x86_64.whl.metadata (9.2 kB)
...
Using cached pillow-10.4.0-cp310-cp310-manylinux_2_28_x86_64.whl (4.5 MB)
...
Installing collected packages: Pillow, ...
Attempting uninstall: Pillow
Found existing installation: pillow 11.1.0
Uninstalling pillow-11.1.0:
Successfully uninstalled pillow-11.1.0
Successfully installed Pillow-10.4.0 ...
done
可以看到,conda 创建环境的流程是先安装 conda 包,再安装 pip 包。在安装 pip 包的时候将依赖冲突的 conda 包卸载了(pillow-11.1.0),并安装了新的版本(Pillow-10.4.0),所以 conda-pack 在打包环境时说“由 conda 管理的文件已被删除/改写”,因此无法打包环境。
可以通过提前将 pip 要安装的包写入 environment.yaml,避免包因为依赖冲突被卸载:
name: idm
channels:
- defaults
dependencies:
- Pillow=10.4.0 # 将 pip 要安装的包写入依赖文件
...
- pip:
...
然后重新创建环境,这样 pip 就不会卸载 conda 包了。

浙公网安备 33010602011771号