Conda环境迁移、备份与恢复

Conda环境迁移、备份与恢复

  • 应用场景
  • `Conda-Pack`
    • 简介
    • 安装
    • 使用
      • 在**源机器**上打包
      • 在**目标机器**上操作
  • 参考链接

应用场景

【场景1】Conda环境迁移

在开发机上通过以下命令创建了一个conda环境:

conda create -n dev python=3.8 xxxxxx

完成开发后,需要在生产机上部署,如何保证生产机与开发机具有完全相同的conda(如果生产机无法访问外网)?

【场景2】Conda环境备份

Conda-Pack

简介

conda-pack 是用于创建conda环境压缩包的命令行工具,可以用该压缩包在其它电脑或路径安装conda环境。尤其在python或conda没有安装的环境中为部署代码准备一致的python环境。

安装

  1. 使用conda进行安装:
    conda install conda-pack
    
    或者
    conda install -c conda-forge conda-pack
    
  2. 从PyPI安装
    pip install conda-pack
    
  3. 从源代码安装
    pip install git+https://github.com/conda/conda-pack.git
    

使用

conda-pack本质上就是一个命令行工具,用来在源机器上打包conda环境并分发到目标机器,后者可能没有安装python和/或conda

源机器上打包

# Pack environment my_env into my_env.tar.gz
$ conda pack -n my_env

# Pack environment my_env into out_name.tar.gz
$ conda pack -n my_env -o out_name.tar.gz

# Pack environment located at an explicit path into my_env.tar.gz
$ conda pack -p /explicit/path/to/my_env

目标机器上操作

# Unpack environment into directory `my_env`
$ mkdir -p my_env
$ tar -xzf my_env.tar.gz -C my_env

# Use python without activating or fixing the prefixes. Most python
# libraries will work fine, but things that require prefix cleanups
# will fail.
$ ./my_env/bin/python

# Activate the environment. This adds `my_env/bin` to your path
$ source my_env/bin/activate

# Run python from in the environment
(my_env) $ python

# Cleanup prefixes from in the active environment.
# Note that this command can also be run without activating the environment
# as long as some version of python is already installed on the machine.
(my_env) $ conda-unpack

# At this point the environment is exactly as if you installed it here
# using conda directly. All scripts should work fine.
(my_env) $ ipython --version

# Deactivate the environment to remove it from your path
(my_env) $ source my_env/bin/deactivate

参考链接

  1. How to clone Python working environment on another machine?
  2. Moving Conda Environments
  3. Conda-Pack

你可能感兴趣的:(Python相关,python,anaconda)