How to Create, Manage, Deploy Conda Virtual Environments!

Conda virtual environments provide users an isolated workspace. It gives you an ability to install your preferred versions of packages without conflicting with other projects and users. Conda environments work in a similar way as PIP environments. However, you will find that Conda is a much better package manager compared to PIP, especially in its ability to resolve non-python dependencies. In this post, we are going to create virtual environments, learn how to use them, install packages within them and how to manage them.

Create Conda Environment

Conda virtual environments are created using conda command line utility. It comes packaged with the Anaconda distribution itself.

Install Anaconda

As a pre-requisite, you’ll need Anaconda installed on your machine. In case you don’t have Anaconda installed, follow the steps given in Getting started with Python post to get Anaconda up and running on your system.

Create a named environment

Let’s cut right to the chase, and create a virtual environment named test-env. Open a terminal and run the following command.

conda create -n test-env

Upon successful execution of the command, you should see similar output as below.

Create Conda Environment
conda create environment

In case you run into an error like ‘conda is not recognized internal or external command‘, verify that you have Anaconda’s bin folder added to PATH.

Create Environment at a given location

Conda gives you an ability to create your virtual environment at a location of your choosing. To do that, instead of the name of the environment, you supply the destination location as an argument to the create command. The later versions of conda will create the destination folders for you if it does not exists already.

conda create -p C:\Users\User\test-env-at-path

Upon the successful execution of the command, you should see output similar to below.

Create Conda Environment
Conda create environment

Activate Conda Environment

If you were able to run commands in the above sections successfully then you have created your first virtual environment. Great! You probably want to start working in it now. To do that you’ll first need to load your environment. Here’s how.

REM Comment: Activate Named Environment
conda activate test-env
REM Comment: Activate Environment create at specified location
conda activate C:/Users/User/test-env-at-path

Once you are done with your work, deactivate the virtual environment using below.

conda deactivate
Conda Activate Environment
Conda Activate Environment

Install Packages using Conda

Once you have the environment created, you can start installing the packages you need. Luckily, Anaconda provides numerous useful Python libraries. You can look up the packages you want using the conda’s search utility. For instance, you can look up if openpyxl package is available or not using below command.

conda search openpyxl

To get an idea, let’s install a few packages in the virtual environments we create before.

REM Comment: First Activate your environment
activate test-env

REM Comment: Search a Package
conda search openpyxl

REM Comment: Install Packages
conda install pandas
conda install numpy
conda install scikit-learn

Here’s how it will look like

install numpy package dependencies
install numpy package dependencies
install numpy in virtual environment
install numpy in virtual environment

You would have noticed that it is trying to install some additional packages that you did not specify. That’s because it’s trying to resolve all the dependencies by installing the dependent packages in your environment. In fact, it includes most basic binaries/libraries like Python, setuptools etc.

Export and Import Virtual Environments

One of the advantages of using virtual environments is, you can re-create exact copies of them very easily. Needless to say, this feature is very useful to deploy your projects in higher environments. So, to achieve this you export the environment specification into a YAML file using conda’s inbuilt environment export utility. The same YAML file can be used to create a new environment with the same spec. Here’s how.

REM Comment: Export Environment Spec
conda env export -n test-env -f test-env-spec.yml

As you must have guessed above command will export your environment specifications into test-env-spec.yml file. Here’s how the output will look like.

Export Conda Environment
Export Conda Environment

Now, the exported YAML file can be used to create a replica of the test-env. You can also ship the YAML file to a different machine and deploy/re-create test-env there.

conda env create -n test-env-replica -f=test-env-spec.yml
Create Conda Environment from YML file
Import Conda Environment

Delete Conda Environment

Once you get used to the concepts of virtual environments, you’ll most likely create a bunch of them. You’d probably have an automation process creating and using virtual environments on the fly. This is when you’d probably want to purge the unwanted environments, you know, to keep things clean. You can achieve this by conda’s remove utility. First, you should list all your environments using the list utility. Here’s how.

REM Comment: List all environments
conda env list

REM Comment: Delete an environment
conda env remove -n test-env
Remove Conda Virtual Environment or Delete Conda Virtual Environment
Remove Conda Environment

Note that conda env list will list all environments. If you want to list all the installed packages then use conda list.

What’s Next?

Hopefully, everything went well till this point, and you have successfully setup a Conda Virtual environment and learned how to use it. It’s always a good practice to work on isolated environments. If you are working on multiple projects that require different collection and versions of packages then the virtual environment is your friend. If you want to explore further then you can test out various options Conda comes with. Try,

conda --help
conda env --help
conda create --help

Finally, hope you find this post useful. Ran into any issues? Want to suggest an edit? Want us to write about a specific topic? We’d love to hear from you. Please leave your feedback in the comments below. Seriously, don’t be shy. 🙂

And yeah, don’t forget to share it with your friends and colleagues. Subscribe to Exit Condition and follow our social media pages to get regular updates. Thanks for stopping by. Happy Learning!