Mastering Conda Environments: A Comprehensive Guide for Python Development

Mastering Conda Environments: A Comprehensive Guide for Python Development

Python virtual environments are one of the most useful development tools for python development. You can have multiple Python virtual environments on the same machine and you could setup different python dependencies in these virtual environments to power your productivity for Python based development.

There are many tools like venv, virtualenv and conda for managing Python virtual environment that provide ability to create many Python virtual environments, install Python dependencies in these environments and delete the virtual environments as needed. In this article, we will look at some of the useful command line options from conda to create, delete and clone Python virtual environments and manage the pip/conda dependencies within those virtual environments.

Prerequisites

You will require the following tools in order to learn different command line options to manage conda environments.

  1. Conda: You could Install conda from Anaconda.

  2. A command line application (like terminal) where you have access to conda command line options.

Once you open the command line terminal you should see the default conda environment which is called the base. Now let's look at some commands from conda that help you manage Python virtual environments.

Check conda version

You could check the current version of the conda environment using the following command:-

(base) >> conda --version

List all conda environments

In order to see all the conda environments configured on your machine you could use the following command:-

(base) >> conda env list

Creating a new conda environment

In order to create a new conda environment, you could use the conda create command in the following way:-

(base) >> conda create --name myenv python=3.8

The above command will create a new conda environment named myenv with the Python version set to 3.8. You could choose the Python version according to your needs.

Activating an existing conda environment

You can activate an existing conda environment (myenv is this case) using the following command:-

(base) >> activate myenv

Activating the conda environment changes the command prompt to your activated environment.

(myenv) >>

Deactivating an existing conda environment

You can deactivate an existing conda environment (myenv is this case) using the following command:-

(myenv) >> deactivate

After you deactivate the environment, you are taken to the base environment.

(base) >>

Installing conda packages

You can install the Python packages from conda-forge using the following command:-

(myenv) >> conda install -c conda-forge <my-conda-package>

Installing pip packages

You can install the Python packages from pypi using pip like you do in other Python virtual environment.

(myenv) >> pip install <my-pypi-package>

Deleting an existing conda environment

You can delete an existing conda (myenv in this case) environment using the following command:-

(base) >> conda env remove --name myenv

You need to deactivate the environment myenv before you try to delete the environment.

Cloning an existing conda environment

You can create an identical conda environment from an existing conda environment using the following command:-

(base) >> conda create --name new_myenv --clone myenv

Taking a snapshot of conda environment

Sometime it is good to take the snapshot of your environment so that you can recreate the same environment on a different machine for reproducibility. One way to create the snapshot of the conda environment is by using conda export command and write the dependencies in a .yml file. For example, if we need to create a snapshot of the all the dependencies in the conda environment myenv, then we could do the following:-

(myenv) >> conda env export > myenv.yml

This captures the dependencies in the file myenv.yml. The contents of the myenv.yml file may look like following:-

name: myenv
channels:
  - defaults
dependencies:
  - ca-certificates=2023.12.12=haa95532_0
  - libffi=3.4.4=hd77b12b_0
  - openssl=3.0.12=h2bbff1b_0
  - pip=23.3.1=py38haa95532_0
  - python=3.8.18=h1aa4202_0
  - setuptools=68.2.2=py38haa95532_0
  - sqlite=3.41.2=h2bbff1b_0
  - vc=14.2=h21ff451_1
  - vs2015_runtime=14.27.29016=h5e58377_2
  - wheel=0.41.2=py38haa95532_0
  - pip:
      - numpy==1.24.4
      - pandas==2.0.3
      - python-dateutil==2.8.2
      - pytz==2023.3.post1
      - six==1.16.0
      - tzdata==2023.3
prefix: C:\Users\gaugup\AppData\Local\anaconda3\envs\myenv

As you could see from the above myenv.yml file, that some environment dependencies are installed from conda and some are installed from pip.

You can recreate the conda environment using the above myenv.yml file using the following conda create command which takes the myenv.yml file as an input:-

(base) >> conda env create -f myenv.yml