Photo by Samuel Zeller

Setting up Conda environments

What Is a Virtual Environment?

       A Virtual Environment is a copy of an existing Python installation with the option to inherit existing packages, and install new ones, without affecting the main Python installation in your Operating System (OS). Two of the main advantages of Virtual Environments are:

  1. Easier backup and recovery from a disaster without affecting any of your Operating System (OS) core dependencies.
  2. You do not need permission to install packages that normally would require you to be root, or a user with administrative powers.

What Is Conda?

       Conda is package manager for Python that can create virtualized development environments and handle your project dependencies without affecting your main Python install.

What Is the Difference Between Conda, Anaconda, and Miniconda?

       Miniconda, is a smaller alternative to Anaconda, which only includes Conda (the package manager) and its bare minimum dependencies.

       Anaconda is a distribution of Conda with a multitude of commonly used Python packages, such as: "numpy," scipy," "ipython notebook," etc.

       Having installed Miniconda, you can easily upgrade to Anaconda by typing in your terminal:

conda install anaconda

How to Install

  1. Download Miniconda for your operating system (OS) from its official distribution page: https://conda.io/miniconda.html.
  2. Run the installer and ensure it completes successfully.
  3. Verify that Conda is installed properly by opening a new terminal and typing:
    conda -V

Ensuring That Conda Is up to Date

       In a terminal, type:

conda update conda

Creating Virtual Environments

       To create a new Virtual Environment from scratch:

  1. Open a new terminal
  2. Navigate to your project directory (folder):
    cd my-project
  3. To create a new Virtual Environment named myvirtenv, using Python version 3.5, and with no default packages, type:
    conda create --no-default-packages -n myvirtenv python=3.5
    Replace myvirtenv for your own custom Virtual Environment name, and 3.5 for the Python version that you would like to use in your project. Remove the flag --no-default-packages to install Conda default packages when creating your environment.

Using an "Environment" File to Create Your Virtual Environment

       Ensure that an environment file, for example environment.yml, exists in your current directory, and then type in your terminal:

conda env create -f environment.yml

Listing All Available Virtual Environments

       Type into your terminal:

conda info --envs

Using the Virtual Environment

       If you are using a Mac or Linux distribution, in order to activate or switch to your Virtual Environment (myvirtenv), type in your terminal:

source activate myvirtenv

       In a Windows machine, use instead:

activate myvirtenv

       Your active Virtual Environment will be displayed between parenthesis or brackets at the at the beginning of your command prompt:

(myvirtenv) $

Installing Additional Packages in Your Virtual Environment

       To install additional packages, for example scipy, in the Virtual Environment that you just created myvirtenv without affecting your main Python install, type:

conda install -n myvirtenv scipy

       If your target environment is currently active (displayed between parenthesis or brackets at the at the beginning of your command prompt) you can just type:

conda install scipy

       You can even install other package managers, such as the well-known pip, and use them to install more packages, for example flask, within your currently activated Virtual Environment:

conda install pip
pip install flask

       Note that you can also use other pip subcommands.

Listing All the Packages Installed in Your Environment

       If the environment myvirtenv is not activated:

conda list -n myvirtenv

       However, if your the environment is active, you can simply type:

conda list

Do you see the pattern? ;)

Exporting Your Virtual Environment to an Environment File

       To export your environment packages to an environment.yml file, you need to activate the environment to export, and then type:

conda env export > environment.yml

       Note that if an environment.yml file already exists in your current directory, it will be overwritten.

Example of a Conda Environment File

       Example of a Conda environment file, for a basic machine learning project named myvirtenv:

Deactivating a Virtual Environment

       To end the session in your currently activated Virtual Environment, type:

source deactivate

       In a Windows machine, just use:

deactivate

Deleting a Virtual Environment with All Its Dependencies

       To delete, for example myvirtenv, with all its dependencies, you would type:

conda remove -n myvirtenv -all

Comments and Feedback