Conda Cheatsheet



View AnacondaCheatSheet from ACG 785016 at Miami Dade College, Miami. ANACONDA CHEAT SHEET See full user documentation for Anaconda docs.continuum.io/anaconda BEFORE YOU. See the conda cheat sheet PDF (1 MB) for a single-page summary of the most important information about using conda.

Check

Managing Conda and Anaconda

conda info

conda update conda

conda update anaconda

Managing Environments

conda info --envs

conda info -e

Active environment shown with *

conda create --name snowflakes biopython

conda create -n snowflakes biopython

conda activate snowflakes

conda deactivate

conda create -n bunnies python=3.4 astroid

conda create -n flowers --clone snowflakes

conda remove -n flowers --all

conda env export > puppies.yml

conda env create -f puppies.yml

Managing Python

conda search --full-name python

conda search -f python

conda create -n snakes python=3.4

Install different version of Python in new environment

Managing .condarc Configuration

conda config --get

conda config --get channels

conda config --add channels pandas

Add a new value to channels so conda looks for packages in this location

Managing Packages, Including Python

conda list

View list of packages and versions installed in active environment

conda search beautiful-soup

Search for a package to see if it is available to conda install

conda install -n bunnies beautiful-soup

Install a new package

NOTE: If you do not include the name of the environment, it will install in the current active environment.

conda update beautiful-soup

conda search --override-channels -c pandas bottleneck

Search for a package in a specific location (the pandas channel on Anaconda.org)

conda install -c pandas bottleneck

Install a package from a specific channel

conda search --override-channels -c defaults beautiful-soup

Search for a package to see if it is available from the Anaconda repository

conda install iopro accelerate

Install commercial Continuum packages

conda skeleton pypi pyinstrument

conda build pyinstrument

Build a Conda package from a Python Package Index (PyPi) Package

Removing Packages or Environments

conda remove --name bunnies beautiful-soup

conda remove beautiful-soup

conda remove --name bunnies beautiful-soup astroid

conda remove --name snakes --all

Package

Notes

  • Based on the cheat sheet from Conda Docs
  • Converted by Charles

Deploying conda environments inside a container looks like a straight-forward conda install. But with a bit more love for details, you can optimise the process so that the build is faster and the resulting container much smaller.

This post intends to be an addition to a Jim Crist-Harif’s “Smaller Docker images with Conda” and a condensed version of the long-read I’ve published at the same time as this post. If you want to understand the reasoning of the tips here, you should head over to the long-read. Otherwise, I hope that this post serves as a cheatsheet to lookup every time you want to put a conda environment inside a (Docker) container.

The dependencies of your to-be-containerised environment should be specified as you typically specify a conda environment using an environment.yml file. An example looks like:

While this is a nice human-readable file, it doesn’t provide you with reproducibility across runs. Thus we pin the requirements down to the actual build of each package using conda-lock. This has the advantage that with the given lockfile, you can recreate the exact environment every time. Also, the lockfiles switch conda into a special mode where it only installs packages but doesn’t do a new solving step. This dramatically improves installation time.

Conda Remove Cheatsheet

To generate a lock file from an environment.yml you can use the following command:

And install it using:

As the base of your images, you should pick one the following three containers that come with a basic / minimal conda installation. In the case of using one of the conda-forge provided images, we always pick the ones that come with mamba included as this speeds up the installation dramatically.

Conda env cheat sheet
  • continuumio/miniconda3: Debian Buster with a minimal conda installation configured for the Anaconda defaults channel (continuumio/miniconda3:4.9.2 is 437MB in uncompressed size)
  • conda-forge/mambaforge3: Ubuntu 20.04 with a minimal conda and mamba installation configured with conda-forge as the default package source (conda-forge/mambaforge:4.9.2-5 is 411MB in uncompressed size)
  • conda-forge/mambaforge-pypy3: Ubuntu 20.04 with a minimal conda and mamba installation configured with conda-forge as the default package source and using PyPy instead of CPython (conda-forge/mambaforge-pypy3:4.9.2-5 is 449MB in uncompressed size)

If you do not plan to use your container interactively but only want to package a service inside of it, you should use multi-stage builds. In the first stage, you should take one of the above containers and build the conda environment in it. As the second stage, you should pick the most minimal container that is required to run the conda environment namely gcr.io/distroless/base-debian10. This container only contains the bare essential like a libc but no shell or package manager at all.

A Dockerfile for this multi-stage approach looks like the following:

This picks up the ideas of Jim’s post. While conda environments come with all batteries included, batteries weigh quite a bit and you don’t need all of them at runtime. Thus you should get rid of those that you know that you will not need.

  • Delete the conda metadata in conda-meta
  • Delete C/C++ includes in include (only required at compile-time)
  • Delete libpython*.so.*; this only works in the case where your entrypoint is calling the python executable which is statically linked, i.e. contains the same symbols as libpython.so. This wouldn’t work if we had an application that would embed the Python interpreter itself.
  • Delete __pycache__ with find -name '__pycache__' -type d -exec rm -rf '{}' '+'
  • Delete pip with rm -rf /env/lib/python3.9/site-packages/pip as we don’t plan to install any packages
  • Delete lib/python3.9/i{dlelib, ensurepip} from the standard library
  • Delete lib{a,t,l,u}san.so as the various sanitizers are not used during production
  • Delete the binaries like x86_64-conda-linux-gnu-ld, sqlite3, openssl from bin. In most cases, these binaries are not used for running a service
  • Delete share/terminfo as we don’t expect to run a terminal

Conda Check Env

The above tips boil down to the following statement. Be aware though that not every file listed here can be safely removed in all cases.

Conda Env Create

Title picture: Photo by Jonas Smith on Unsplash