pythonconda

is there any script to convert conda list to yml file


I have a saved file with the output of conda list of my previously created environment. It is of the format as below

# packages in environment at /home/*****/intelpython3:
#
# Name                    Version                   Build  Channel
_tflow_select             2.1.0                       gpu    anaconda
absl-py                   0.8.0                    py36_0    anaconda
affine                    2.3.0                    pypi_0    pypi
asn1crypto                0.24.0                   py36_3    intel
astor                     0.8.0                    py36_0    anaconda
atomicwrites              1.3.0                    pypi_0    pypi
attrs                     19.3.0                   pypi_0    pypi
audioread                 2.1.6                    py36_0    <unknown>
awscli                    1.16.292                 pypi_0    pypi
backcall                  0.1.0                    py36_2    <unknown>
backports                 1.0                      py36_9    <unknown>
bayesian-optimization     1.0.1                    pypi_0    pypi
bleach                    2.1.3                    py36_2    <unknown>

Can anybody help me with some python/unix script or some other way to convert this to a yaml (environment.yml) file which can be used with conda to create a new environment


Solution

  • To convert your exported file to a yaml spec for conda, you can just join the Name and Version columns with and equal sign. Pandas is a good tool for this (I assumed your current environment list is named 'environment.txt'):

    import pandas as pd
    
    df = pd.read_table('environment.txt', sep='\\s+', skiprows=3, header=None, 
                       names=['Name', 'Version', 'Build', 'Channel'])
    env = df.Name + '=' + df.Version
    env.to_csv('environment.yml', header=False, index=False)
    

    The build number won't be included, but conda doesn't really need those. You will have to specify the channels yourself.


    To create a yaml file of your current environment, you need the --export flag and a pipe to a file.

    conda list -n intelpython3 --export > environment.yml
    

    To use the file you can run:

    conda env create -n <environment name> --file environment.yml
    

    Newer versions of conda can also just use:

    conda create -n <environment name> --file environment.yml
    

    but there were some issues on older versions, at least on Windows.