Automate Anaconda Environment creation


Taking the manual steps and creating automation

Taking the manual steps and create automation

Creating a virtual environment in Anaconda is a straight forward task using the conda create -n env-namme command. By default, the new environment will be created in the folder of the anaconda/miniconda installation. For example in a Linux machine the environments are under the directory of ~/miniconda3/envs. Now, if we are working in projects that involve creating multiple conda environments, it might become tedious to always creating this environments with the manual way, installing each and every package by sequentially type the commands on the terminal. Of course we can add the most used packages to the ~/.condarc file and save some time. But, the other day I had to create, again, a virtual environment and I thought that it might be useful to automate as much as possible I can the procedure of crating virtual environments.


Designing the conda-env-automation

  1. To create a virt env we use the conda create -n test-env command. Since the script would be asking the user for the name of the env, need to have a user input for the name of env.
echo env_name
  1. Since, we use anaconda environments to work with different python versions and packages, need to have a input that determines the version of Python as well as the packages will be install during the environment creation.
conda create -n test-env python=3.x package1 package2
  1. Also can ask to activate the newly created environment
conda activate test-env

Optional operations

  1. Create an export (backup file) of the environment. This is helpful if someone else want to reproduce our work using the same environment.
conda test_env export > environment_backup.yml

A diagram representation of the steps

Code Implementation

Initially will set a variable name for the location of the environments directory in the miniconda location. Also, will give the list of any existing environments.

    echo "The folowing environments are installed:$(ls $ENV_PATH)"

Then, will check if the environment we like to crate exists and if so the process will exit the script

    if [ -d "$dir_path" ];
        echo "$env_name environment exists."
        slee 1
        exit 0
        echo "$env_name environment does not exist."
        sleep 1
        echo "Will create the $env_name conda environment"

The dir_path variable gives the location of the environment, if that exists in the envs folder, The if statement looks for the particular environment, and if that exists will exit the script, if not the will continue to create the environment.

The conda environments are used in the most of the cases to have a specific python version. So, the next part will ask for what python version of python we want.

read -rp "Enter the Python version(only number): " py_ver

Also, most of the times we install particular packages will we create the environment. This can be done with following

read -rp "Enter additional packages to install: " packages

Now that we have the python version and the packages we can create the environment

conda create -n $env_name python=$py_ver $packages -y

The environment now has been crated and can use it by activating it with the conda activate test-env for example. We can further automate the process and prompt a question if we want to use the env after the creation. The script will ask if we want to activate the environment, if yes specified then will enter to it and if no will continue to the next step, witch is to create an export of the environment.

read -rp "Do you like to activate $env_name environemnt?" ask &&
        if [ "$ask" = "y" ]; then
            printf "Activating the environment...\\n"
            conda activate $env_name

Sometimes we might need to use a specific environment to another computer, or to send it to colleagues in our team for example to replicate the work we've done. This can be done if we export the .yml of the environment. So, the process can be automated to export the basic environment created initially. The script will ask if we want a backup file, if no the script will exit. If we do want to create a backup file of the environment then need to activate it and export the environment. Last, will deactivate the environment and exit the script.

read -rp "Do you want to create a backup for the $env_name?" ask &&
        if [ "$ask" = "y" ]; then
            printf "Creating the backup.yml\n"
            conda activate $env_name && conda env export > "$env_name.yml"
            conda deactivate
            echo "The backup file crated."
            exit 0