Skip to content

Instantly share code, notes, and snippets.

@keuv-grvl
Created February 18, 2021 14:56
Show Gist options
  • Save keuv-grvl/807ae0609ba9f34ce8503bb1c9a544d0 to your computer and use it in GitHub Desktop.
Save keuv-grvl/807ae0609ba9f34ce8503bb1c9a544d0 to your computer and use it in GitHub Desktop.
Autoactivate Conda env when entering a directory (using dir name or provided name).
# ln -s /path/to/this/.autoenv.sh /path/to/workdir/
if test -f .autoenv_conda_env_name ; then
TARGET_CONDA_ENV="$(cat .autoenv_conda_env_name)"
if [[ $TARGET_CONDA_ENV != $CONDA_DEFAULT_ENV ]]; then
conda activate "$TARGET_CONDA_ENV" 2&>1 2> /dev/null
fi
else
CUR_DIR=$(basename $(pwd))
conda activate "$CUR_DIR-dev" 2> /dev/null \
|| conda activate "$CUR_DIR-env" 2> /dev/null \
|| echo "❌ no conda env found using names '"$CUR_DIR"[-dev|-env]'.\nWrite the env name to activate to a '.autoenv_conda_env_name' file"
fi
# ln -s /path/to/this/.autoenv_leave.sh /path/to/workdir/
if [[ $CONDA_DEFAULT_ENV != "base" ]] ; then
conda deactivate
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment