Image from Wikimedia Commons

Anaconda Cheat Sheet

Table of Contents

This cheat sheet will help you get started with the Anaconda distribution, a popular platform for data science. Anaconda is a free and open-source distribution that includes a wide range of tools for data science, such as Jupyter Notebooks, NumPy, and pandas. This cheat sheet will help you get started with Anaconda and make the most of its features.

Quick and Useful Commands

  • conda -v See current version of Anaconda
  • conda install [package] Install package to current environment
  • conda install -c [channel] [package] Install package with a specific channel
  • conda search python See available versions of Python
  • conda list List all packages installed in current environment
  • conda update conda Update Anaconda
  • conda info Environment information
  • conda info -e or conda env list List all installed enviroments
  • conda env export > environment.yml Export active environment
  • conda env create -f environment.yml Create environment from specification
  • conda install --rev 1 Restore root environment to its state after installation
  • conda remove --name myenv --all Remove an environment
  • conda clean --all Remove unused packages and caches for all environments

How to use Virtual Environments in Anaconda?

In Anaconda it is possible to run different environments and versions of Python which helps when working with conflicting packages or packages that are for example not updated and available for newer Python versions. In order to install a new environment with the name py35 in Anaconda simply use

conda create -n py35 python=3.5 anaconda

This creates a new environment installed in the /envs/py35 directory inside the Anaconda directory. If you need to install further packages to the created virtual environment you can install it by

conda install -n py35 [package]

This virtual enviroment can be now activated with

source activate py35

or on windows with activate py35. You can then deactivate the environment with

source deactivate py35

or on windows with deactivate py35. And finally if you do not need the environment anymore, it can be deleted with

conda remove -n py35 -all

Here is more information on how to manage python and on how to manage packages in Anaconda.

Anaconda Base Environment

Check if Anaconda base environment is enabled:

conda config --show | grep auto_activate_base

To disable the Anaconda base environment:

conda config --set auto_activate_base False

Reset Anaconda Root Environment

Remove root environment and conda command:

conda install --revision 0

Restore root environment to state after first installation:

conda install --revision 1

To list revisions, type:

conda list --revision

Source: How to reset anaconda root environment

Export Anaconda Environment

Export the current Anaconda environment:

conda env export \
  --from-history \
  --no-builds > myenv.yml

The --no-builds flag prevents the inclusion of platform-specific build IDs. When using --from-history (introduced in Conda 4.7.12), Conda will only export packages and versions which you have explicitly installed using conda install.

This environment can be then installed with:

conda env create \
  --name myenv \
  --file myenv.yml

Here is a script to bulk export all conda environments in your system:

#!/bin/bash
set -e

conda activate
for conda_env in $(conda env list | cut -d" " -f1 | tail -n+4); do
  echo $conda_env
  conda activate $conda_env
  conda env export \
    --from-history \
    --no-builds > "${conda_env}.yml"
done
conda activate

This needs to be run with source script.sh.

References