conda export environment and conda import environment

conda export and import feature is ideal functionality to build a predefined environment from a list in a text file.
Here are some caveats (or features), which may stumble the user to build a working conda environment list file:

  • There are packages, which are not available for all OS platforms. There are packages, which are only available in Linux platforms and other only under Windows platform!
  • There are package names, followed by version and build version. All three a valid entries in the list file – only the name of the package, the name of the package with version and the name of the package with version and build version. For example,
      - setuptools=58.0.4=py38h06a4308_0
      - sqlite=3.37.0=hc218d9a_0
      - tk=8.6.11
      - wheel
    
  • Builds’ versions are specific for the OS and they are different for every Operating systems.
  • Packages’ versions do tend to deprecate, so the old environment may not be possible to replicate because of a missing package version. Exported list with version, which are unavailable any more and so it cannot be imported.
  • A good practice is to update the current working environment with the latest updates before exporting it.
  • Export environment list without build versions. Edit the exported environment list if some version is missing. The version of the packages could be removed, too.
  • The exported environment list uses yaml format.

1) Here is the command to export an environment list of a python environment with and without builds and versions of the packages:

  1. With builds versions
    (base) myenv@srv ~ $ conda env export -n mypython37
    name: mypython37
    channels:
      - defaults
    dependencies:
      - _libgcc_mutex=0.1=main
      - _openmp_mutex=4.5=1_gnu
      - ca-certificates=2021.10.26=h06a4308_2
      - certifi=2021.10.8=py37h06a4308_2
      - ld_impl_linux-64=2.35.1=h7274673_9
      - libffi=3.3=he6710b0_2
      - libgcc-ng=9.3.0=h5101ec6_17
      - libgomp=9.3.0=h5101ec6_17
      - libstdcxx-ng=9.3.0=hd4cf53a_17
      - ncurses=6.3=h7f8727e_2
      - openssl=1.1.1m=h7f8727e_0
      - pip=21.2.2=py37h06a4308_0
      - python=3.7.11=h12debd9_0
      - readline=8.1.2=h7f8727e_1
      - setuptools=58.0.4=py37h06a4308_0
      - sqlite=3.37.0=hc218d9a_0
      - tk=8.6.11=h1ccaba5_0
      - wheel=0.37.1=pyhd3eb1b0_0
      - xz=5.2.5=h7b6447c_0
      - zlib=1.2.11=h7f8727e_4
    prefix: /home/myenv/miniconda3/envs/mypython37
    

    By default, the output is in the console with YAML syntax. There is a JSON option and a file option to output it in a file:

    conda env export -n mypython37 -f ./mypython37.yml
    
    (base) myenv@srv ~ $ conda env export -n mypython37 -f ./mypython37.yml
    (base) myenv@srv ~ $ conda env export -n mypython37 --json
    {
      "channels": [
        "defaults"
      ],
      "dependencies": [
        "_libgcc_mutex=0.1=main",
        "_openmp_mutex=4.5=1_gnu",
        "ca-certificates=2021.10.26=h06a4308_2",
        "certifi=2021.10.8=py37h06a4308_2",
        "ld_impl_linux-64=2.35.1=h7274673_9",
        "libffi=3.3=he6710b0_2",
        "libgcc-ng=9.3.0=h5101ec6_17",
        "libgomp=9.3.0=h5101ec6_17",
        "libstdcxx-ng=9.3.0=hd4cf53a_17",
        "ncurses=6.3=h7f8727e_2",
        "openssl=1.1.1m=h7f8727e_0",
        "pip=21.2.2=py37h06a4308_0",
        "python=3.7.11=h12debd9_0",
        "readline=8.1.2=h7f8727e_1",
        "setuptools=58.0.4=py37h06a4308_0",
        "sqlite=3.37.0=hc218d9a_0",
        "tk=8.6.11=h1ccaba5_0",
        "wheel=0.37.1=pyhd3eb1b0_0",
        "xz=5.2.5=h7b6447c_0",
        "zlib=1.2.11=h7f8727e_4"
      ],
      "name": "mypython37",
      "prefix": "/home/myenv/miniconda3/envs/mypython37"
    }
    

    The first line uses “-f” to save the output to a file and the second line uses json option to output the environment file in JSON format.

  2. Without builds versions. Use the “–no-builds” option:
    (base) myenv@srv ~ $ conda env export -n mypython37 --no-builds
    name: mypython37
    channels:
      - defaults
    dependencies:
      - _libgcc_mutex=0.1
      - _openmp_mutex=4.5
      - ca-certificates=2021.10.26
      - certifi=2021.10.8
      - ld_impl_linux-64=2.35.1
      - libffi=3.3
      - libgcc-ng=9.3.0
      - libgomp=9.3.0
      - libstdcxx-ng=9.3.0
      - ncurses=6.3
      - openssl=1.1.1m
      - pip=21.2.2
      - python=3.7.11
      - readline=8.1.2
      - setuptools=58.0.4
      - sqlite=3.37.0
      - tk=8.6.11
      - wheel=0.37.1
      - xz=5.2.5
      - zlib=1.2.11
    prefix: /home/myenv/miniconda3/envs/mypython37
    

2) Import conda environment file.

Import the conda environment file above by executing the following commands.
First, create the conda environment if missing.

(base) myenv2@srv ~ $ cat ./mypython37.yml
name: mypython37
channels:
  - defaults
dependencies:
  - _libgcc_mutex=0.1
  - _openmp_mutex=4.5
  - ca-certificates=2021.10.26
  - certifi=2021.10.8
  - ld_impl_linux-64=2.35.1
  - libffi=3.3
  - libgcc-ng=9.3.0
  - libgomp=9.3.0
  - libstdcxx-ng=9.3.0
  - ncurses=6.3
  - openssl=1.1.1m
  - pip=21.2.2
  - python=3.7.11
  - readline=8.1.2
  - setuptools=58.0.4
  - sqlite=3.37.0
  - tk=8.6.11
  - wheel=0.37.1
  - xz=5.2.5
  - zlib=1.2.11
prefix: /home/myenv/miniconda3/envs/mypython37
(base) myenv2@srv ~ $ conda create --name mypython37
Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /home/myenv2/miniconda3/envs/mypython37



Proceed ([y]/n)? y

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate mypython37
#
# To deactivate an active environment, use
#
#     $ conda deactivate

Import, i.e. update the conda environment with the environment file.

(base) myenv2@srv ~ $ conda env update -n mypython37 --file ./mypython37.yml 
Collecting package metadata (repodata.json): done
Solving environment: done

Downloading and Extracting Packages
setuptools-58.0.4    | 775 KB    | ############################################################# | 100% 
wheel-0.37.1         | 33 KB     | ############################################################# | 100% 
certifi-2021.10.8    | 151 KB    | ############################################################# | 100% 
python-3.7.11        | 45.3 MB   | ############################################################# | 100% 
pip-21.2.2           | 1.8 MB    | ############################################################# | 100% 
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate mypython37
#
# To deactivate an active environment, use
#
#     $ conda deactivate

(base) myenv2@srv ~ $ conda activate mypython37
(mypython37) myenv2@srv ~ $ python -V
Python 3.7.11

The import of the environment lists generated with builds versions of the packages is the same as without builds. “conda env update” would try to resolve the requested files and if they are missing an error will be shown, if not the environment will be updated according to the environment file.

3. Fail to import an environment because of unresolvable files.

Solving environment: failed.

Exported environment list from a Windows host and trying to import it under Linux. It fails with missing files because there are no such files for Linux distributions (even the file has no build version and package version):

(base) myenv@srv ~ $ cat ./windows-flow.yml
name: windows-flow
channels:
  - defaults
dependencies:
  - _tflow_select=2.3.0
  - pyreadline=2.1
  - vc=14.2
  - vs2015_runtime
  - win_inet_pton=1.1.0
  - wincertstore=0.2
  - pyreadline=2.1
  - werkzeug=2.0.2
  - wheel=0.37.0
  - wrapt=1.12.1
  - xz=5.2.5
  - yarl=1.6.3
  - zipp=3.6.0
  - zlib=1.2.11
  - zstd=1.4.9
prefix: C:\Users\myenv\Anaconda3\envs\windows-flow
(base) myenv@srv ~ $ conda env update --name test_env -f ./windows-flow.yml 
Collecting package metadata (repodata.json): done
Solving environment: failed

ResolvePackageNotFound: 
  - wincertstore=0.2
  - vs2015_runtime
  - vc=14.2
  - pyreadline[version='2.1.*,2.1.*']
  - win_inet_pton=1.1.0

(base) myenv@srv ~ $

The package vs2015_runtime cannot be resolved even without any version!

Missing build and after removing the builds information in the environment file the import is successful:

(base) myenv@srv ~ $ cat test-env-builds.yml
name: test-env-builds
channels:
  - defaults
dependencies:
  - _tflow_select=2.3.0=mkl
  - cryptography=3.4.8=py38h71e12ea_0
prefix: /home/myenv2/miniconda3/envs/test-env-builds

(base) myenv@srv ~ $ conda env update --name test_env -f ./test-env-builds.yml 
Collecting package metadata (repodata.json): done
Solving environment: failed

ResolvePackageNotFound: 
  - cryptography==3.4.8=py38h71e12ea_0
  
(base) myenv@srv ~ $ cat test-env-builds.yml
name: test-env-builds
channels:
  - defaults
dependencies:
  - _tflow_select=2.3.0=mkl
  - cryptography=3.4.8
prefix: /home/myenv2/miniconda3/envs/test-env-builds
(base) myenv@srv ~ $ conda env update --name test_env -f ./test-env-builds.yml 
Collecting package metadata (repodata.json): done
Solving environment: done

Downloading and Extracting Packages
python-3.9.7         | 18.6 MB   | ############################################################# | 100% 
cryptography-3.4.8   | 946 KB    | ############################################################# | 100% 
pip-21.2.4           | 1.8 MB    | ############################################################# | 100% 
_tflow_select-2.3.0  | 2 KB      | ############################################################# | 100% 
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate test_env
#
# To deactivate an active environment, use
#
#     $ conda deactivate

Sometimes even the package is available but not the version mentioned in the environment file, so by removing the package version, the environment list would be imported.

Leave a Reply

Your email address will not be published.