Creating an environment in anaconda through a yml file

Sachinjose
3 min readJan 7, 2021

Conda environments are independent containers, because of which the package and their version inside will not impact the packages that are outside the environment. creating a virtual environment helps you to play around the packages and dependencies by keeping it isolated from the rest of your code.

We can use yml to create an environment manually or export from the existing environment.

The instructions given in the post are for Windows and written presuming that anaconda has been already installed

Step 1: Building an environment manually

If you want to be more organized and efficient by separating environments for different projects that might require specific libraries, anaconda allows you to easily create environments manually through yml files.

The yml file contains information for the environment you wish to create in the below parts:

Fig 1.
  • Name of environment
  • Dependencies, such as the libraries you wish to pre-install when creating the environment.
  • channels(Optional), refer to channels to download the packages. Packages on conda-forge may be more up-to-date than those on the defaults channel.
Fig 2.
Fig 2.

If you want to include some pip package, you can do that too.

As shown in Fig 2. you can add the packages that need to be installed using pip, whereas other packages will be installed using conda.

As shown in fig 2. if any specific version of the library is needed, you have to mention it, else the latest version available will be downloaded.

Step 2: Installing the libraries

After creating a yml file, run the below command. Make sure you have been outside any conda environments.

conda env create -f <path_to_yaml_file>

This will create an environment and install all the dependencies mentioned.

Step 3: Activating the environment

To check whether the environment is created, run the below command.

conda env list

This will list all the environment created.

conda activate <env_name>

This command will activate the environment as shown below.

Step 4: Exporting the Environment file

First, let's activate the environment you want to export

conda env export > environment.yml

This will handles both the conda and pip packages in the environment. This yml can be shared with others to create the same environment as yours

Step 5 (Optional): Including the environment in the Jupyter notebook

By including Ipykernel library in the dependencies and then adding kernel to jupyter notebook, we can use the environment in the jupyter notebook.

For creating virtual environments in anaconda and using it inside jupyter, see here

That’s it for the article. Thanks for reading this, I hope it’ll be useful.

Thank you and stay safe!

--

--