A cognitive load shortcut for conda activate

How I activate my Conda environments without disrupting my flow
conda
python
cognitive load
Author

Matt Fisher

Published

March 8, 2024

Whenever I need to activate a Conda environment, I struggle to remember the name of the environment I need to activate. It doesn’t take too long too remember usually, a couple of seconds, but that often significantly disrupts an ongoing thought process or, if I’m pair programming, a conversation. Last I checked, tab completion support was removed from Conda, and in any case, my issue is remembering the name, not typing too much! But by the end of this post, I’ll get us down to three-keystroke environment activation.

It took me 8 years, but today I realized this is a cognitive load problem that I can solve relatively easily.

From now on, I will always name my Conda environments after the Git repo root directory (this should match the GitHub repository name). With the exception of projects with multiple environment files, I’ll no longer need to remember environment names.

For this project:

environment.yml
name: "mfisher87.github.io"
# ... etc.

This solves the memory problem, but we can do a bit better.

We can activate an environment named after the current directory:

conda activate $(basename $PWD)

Using an alias I already have (read on for details), we can activate an environment named after the current Git repository root directory from any subdirectory within that repo:

conda activate $(basename $(git root))

And finally, a two-character alias I put in my ~/.bash_aliases (sourced from ~/.bashrc; see my dotfiles repository):

# Activate a Conda environment named after root directory name of the current
# Git project. Of course, only works if you have named your envs to match your
# project names (and keep the repo in a directory named after the project)!
alias ca='conda activate $(basename $(git root))'

Here’s the configuration from my ~/.gitconfig that provides the git root command:

[alias]
  # Output the path of the root directory of this git repo:
  root = "rev-parse --show-toplevel"

Citation

BibTeX citation:
@online{fisher2024,
  author = {Fisher, Matt},
  title = {A Cognitive Load Shortcut for `Conda Activate`},
  date = {2024-03-08},
  url = {https://mfisher87.github.io/posts/conda-activate-cognitive-load},
  langid = {en}
}
For attribution, please cite this work as:
Fisher, Matt. 2024. “A Cognitive Load Shortcut for `Conda Activate`.” March 8, 2024. https://mfisher87.github.io/posts/conda-activate-cognitive-load.