Conda Environments#

Conda environment is standard practice in Python to isolate installation of Python with associated packages. With environments, one project can have a separate version of Python and packages, and avoid the package conflict of versions.

Conda is a popular solution to handling multiple environments across multiple projects. Additionally, conda acts as its own package manager and allows you to download packages from multiple channels outside PyPI.

Creating a conda Environment#

Create a conda environment with the Anaconda Prompt:

> conda create --name env_name python=3.7
> conda activate env_name

Installing Packages with conda#

Now, let’s install some packages. Instead of using pip, we’ll use conda instead.

>conda install pytorch

Next, you’ll notice that conda takes some time while it is in the “solving environment” step. This is done to ensure that all packages are strictly compatible with each other. Conda automatically determines the best versions to use of whatever necessary packages are being downloaded.

Listing Environments#

To show all the environments installed on your system, use conda env list.

Switching Environments#

To switch environments, simply use conda activate <env_name>. Then, use conda list to show all the packages installed in that environment.

Exporting an Environment#

You can define an environment by using a environment.yml file. This file contains information about the name of the environment, which packages are installed to the environment, and what channels the environment is using. To create an environment.yml file, use:

> conda env export > environment.yml

Importing an Environment#

To create an environment from a environment.yaml file, use

> conda env create -f environment.yml