ruk·si

🐍 Anaconda

Updated at 2020-02-05 16:16

conda is a language-agnostic package and environment manager. Conda makes it easy to use Python, R, Fortran, etc. libraries.

conda is available in two widely known distributions:

  • Anaconda is a data science distribution with Conda and various data science tools included. It focuses on making data science more accessible to everybody.
  • miniconda is a distribution with only the conda package management included so you will have more control what is being installed.

Installing conda:

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
chmod 0700 Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
# enter, yes, enter, yes, restart terminal

Configuring conda:

conda info
conda update -y conda
# don't activate the base environment by default
conda config --set auto_activate_base false
vim ~/.condarc
conda config --describe  # to see all available configuration

Creating a conda environment for a new project:

conda create -y -n conda-example python=3.7
conda env list
mkdir ~/Projects/conda-example
cd ~/Projects/conda-example
conda activate conda-example
# conda deactivate

Saving current environment state to disk

conda env export --no-builds  | grep -v "^prefix: " > environment.yaml

Syncing your local environment with the one defined on disk.

conda env update --file environment.yaml --prune

Create a new conda environment based on a project environment file.

conda env create --force -n conda-example -f environment.yaml

Let's create a small Python script (check.py) to check if libraries are installed:

import numpy as np
import pandas as pd

print(np.__version__)
print(pd.__version__)

You run commands with conda run:

conda run -n conda-example python check.py

You can define new dependencies inside or outside the environment. export calls in the following are to save changes to environment.yaml to sync future setups:

# outside the environment
conda install -n conda-example numpy
conda env export -n conda-example --no-builds  | grep -v "^prefix: " > environment.yaml
conda run -n conda-example python check.py

# inside the environment
conda activate conda-example
conda env export --no-builds  | grep -v "^prefix: " > environment.yaml
conda install pandas
python check.py

# as an extra, here is how you would use `pip`
conda install -n myenv pip
conda env export -n conda-example --no-builds  | grep -v "^prefix: " > environment.yaml
conda run -n pip list

You can nuke the conda environment if something goes wrong:

conda env remove -n conda-example
conda env create --force -n conda-example -f environment.yaml

Source