Summary
This post describes the utility, creation, and maintenance of conda environments.
Assumptions
- Anaconda or Miniconda is installed on your computer (version > 4.4)
- You have a basic familiarity with linux/unix commands
Problem
Have you ever had trouble getting an old script to run? Or have you tried to pass code to a colleague and spent hours figuring out which packages and versions are needed? Conda environments will help you solve these problems!
Definition
Conda environments are groups of packages that are organized into directories and can be independently accessed.
Introduction
If you have installed Anaconda or Miniconda you already have an environment called base. Type conda env list to see the list of conda environments on your computer.
Example:
# conda environments:
#
base * /Users/stephanie/anaconda
data_science /Users/stephanie/anaconda/envs/data_science
- The name on the left (in the example
baseanddata_science) are the names of the conda environments - The
*indicates the conda environment that is currently activated - The path represents the path where the conda environment can be accessed
Within a given environment, you can examine the contents by typing:
conda list
Set up
One of the best ways to create and maintain an environment is to use a yaml file. Below as an example of a conda environment yaml file. Save the text below to a file called environment.yml.
name: python_env
channels:
- conda-forge
- defaults
- mro
dependencies:
- python=3.7.*
- pandas=0.23.*
- pip:
- snowflake-connector-python==1.7.*
nameis the title of the conda environmentchannelis the place where packages are installeddependenciesare the list of packages to conda install in the environment- formatted: <package_name>=<version_number>
- The
*means give me the last version (i.e. python 3.7.* will give me python 3.7.2 as of 2/2/19) - add
pipas a dependency and put packages that can only be installed throughpip install. Note that the format for pip package installation is <package_name>==<version_number>
In the command line run:
conda env create -f environment.yml
If you run conda env list you will now see the new environment
# conda environments:
#
base * /Users/stephanie/anaconda
data_science /Users/stephanie/anaconda/envs/data_science
python_env /Users/stephanie/anaconda/envs/python_env
To activate the environment run:
conda activate python_env
Your prompt will have the name of the environment in parentheses. This is set in your conda config file. If you would like to add or remove this run conda config --set changeps1 True or False (set True to add it and False to remove it)
(python_env) stephanie:~ Stephanie$
To deactivate the environment run:
conda deactivate
Maintenance
If you would like to add packages to your environment instead of conda install or pip install edit your environment.yml file.
For example : if we want to add matplotlib to the python_env open the file and add the package under dependencies:
name: python_env
channels:
- conda-forge
- defaults
- mro
dependencies:
- python=3.7.*
- pandas=0.23.*
- matplotlib=3.0.*
- pip:
- snowflake-connector-python==1.7.*
To update your environment to include matplotlib run:
conda env update -f environment.yml
To remove the python_env environment, deactivate the environment and type:
conda env remove --name python_env
Advanced Settings
Configure conda
To show your conda configuration:
conda config --show
To learn more about each configuration go to .condarc docs
Notable Configurations
always_yes: True– If you set to True it will always put yes when installingauto_update_conda: True– If True conda will update conda every time you install packages in your base environment. If False you have to update conda manually.
Run a specific conda environment at startup: In your .bash_profile after the conda initialization add:
conda activate <environment_name>
In a jupyter notebook / lab you can print which conda environment is activated.
! echo $CONDA_DEFAULT_ENV
direnv
direnv allows you to switch environments depending on which directory you open
Installation
brew install direnv
Getting Started
In your bash_profile add:
show_conda_env() {
if [[ -n "$CONDA_DEFAULT_ENV" && -n "$DIRENV_DIR" ]]; then
echo "($(basename $CONDA_DEFAULT_ENV))"
fi
}
export -f show_conda_env
PS1='$(show_conda_env)'$PS1
eval "$(direnv hook bash)"
In the directory where you want a specific conda environment to activate create a .envrc file
source "/Users/stephanie/anaconda/etc/profile.d/conda.sh"
conda activate python_env
- The first line is sourcing the conda commands.
- The second line is activating the environment
Now when you cd into that folder it should activate the python_env environment