Managing Multiple Environments in Anaconda | Machine Learning

Original article was published by Pranjal Saxena on Artificial Intelligence on Medium

Managing Multiple Environments in Anaconda | Machine Learning

Manage Multiple Anaconda Environments

Photo by Soragrit Wongsa on Unsplash

Things look good when they are appropriately organized. And, Same goes with data science. Anaconda comes up with Jupyter Notebook, and we usually prefer using the Jupyter Notebook for our data science tasks. Tasks can be of the training model, preparing data, feature engineering, plotting graphs, validating model or testing our trained model. When we work on different machine learning and deep learning model and therefor specific models, we need a set of libraries to use. And, If we will make all the libraries installed in a single environment, then we might end up with many issues.

Those issues can be related to the version of the library used or any other compatibility issue. And, most notably, when we migrate our model to the different Environment like the cloud or different machine, then it becomes harder to know what libraries we used for our task. Here comes the need for creating different anaconda environment.

When we have different anaconda environment, then it is essential to manage them correctly, so that we can utilize then later when similar projects come up. In this article, I will cover some prominent points of managing of anaconda environments. Here, I assume that you have Anaconda installed in your machine.

Creating Your First Anaconda Environment

After installing Anaconda, you can find Anaconda Prompt in your search window. Click on that, and you will get the Anaconda Prompt.

Anaconda Prompt

Here, We will create a new environment named “Python-Keras”. The name of the Environment should be based on your project so that we can use it later.

conda create -n Python-keras python=3.6

After running the command, you will get the below screen where it will start creating an environment.

Creating Environment

Our Environment gets created, but it is not having any library installed. We can install them after activating the Environment. And, there is another way so that you can install libraries while creating the Environment.

For that, we need to create the YML file and that YML file contains the name of the Environment and the libraries used.

YML file for Environment

Now, we can use the below command that can create the Environment using the yml file.

conda env create -f python39.yml

Here, an environment will get created with the defines libraries.

Activating Your Environment

After creating your Environment if we want to you that Environment for Jupyter Notebook then we need to activate it. Use the below command to activate that Environment.

activate Python-Keras
Activating Environment

We can also install other libraries after activating the Environment. We can use PIP to instal and also we can use the “conda” to install the libraries.

Installing Library in Environment

Sharing Your Created Environment

After activating your Environment, we can share our Environment so that we can use the same project in different locations or machine. Use the below command to generate the YML file that can be shared with others.

conda env export > pythonkeras.yml
Sharing Environment

It will generate a YML file in the same command prompt location. In this case, it will save in “C:\Users\pranj”.

Getting a list of Environments

After completing your task, to work on different projects, we switch to a different environment. And, to do that first we need to check for available environments. Use the below command for the same.

conda info --envs
List of Environments

I have many environments created; we can use activate command to switch to a different environment.

Switching Between Environments

Deleting Your Environment

When you think that you don’t need any particular environment then instead of keeping, then we can free them from the hard disk. To do that, Anaconda provides a command.

conda remove --name Python-Keras --all

First of all, you need to exit from the activated Environment by opening Anaconda prompt again.

Deleting Environment