Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # automatically activate conda environment when entering folder with environment file
- # if env doesn't yet exist, create it
- # deactivate env when exciting said folder
- # installation: copy contents to .zshrc or save file and source it in .zshrc
- precmd() {
- for FILE in environment.yml env.yml requirements.yml; do
- if [ -e $FILE ]; then
- ENV=$(sed -n 's/name: //p' $FILE)
- # Check if env is already active
- if [[ $PATH != *$ENV* ]]; then
- conda activate $ENV
- # Check if env activation was unsuccessful, in that case create new env from file
- if [ $? -ne 0 ]; then
- echo "Conda environment '$ENV' doesn't exist. Creating it now."
- conda env create -q
- conda activate $ENV
- fi
- CONDA_ENV_ROOT="$(pwd)"
- fi
- elif [[ $PATH = */envs/* ]]\
- && [[ $(pwd) != $CONDA_ENV_ROOT ]]\
- && [[ $(pwd) != $CONDA_ENV_ROOT/* ]]
- then
- CONDA_ENV_ROOT=""
- conda deactivate
- fi
- done
- }
Add Comment
Please, Sign In to add comment