Creating an environment in anaconda through a yml file

Photo by 🇸🇮 Janko Ferlič on Unsplash

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.

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!




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Zip function in Python

Exploring Some FAQs on the Aleph Zero Testnet Launch

5 reasons why to use Finder tags on Mac

Inhouse QA v/s Outsourced QA : Make The Right Choice

Inhouse QA v/s Outsourced QA : Make The Right Choice

Circular dependency detected while autoloading constant ____

Software developers lack discipline

Load Testing | Best Practices For Black Friday

How Does Alibaba Implement CI/CD Based on Kubernetes

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


More from Medium

Learn the basics of Operating systems for Devops

ORM (Object Relational Mapping)

Setup python development environment with VSCode.

How do I create a service for a shell script ?