Guest User

Untitled

a guest
Dec 10th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. # automatically activate conda environment when entering folder with environment file
  2. # if env doesn't yet exist, create it
  3. # deactivate env when exciting said folder
  4. # installation: copy contents to .zshrc or save file and source it in .zshrc
  5. precmd() {
  6. for FILE in environment.yml env.yml requirements.yml; do
  7. if [ -e $FILE ]; then
  8. ENV=$(sed -n 's/name: //p' $FILE)
  9. # Check if env is already active
  10. if [[ $PATH != *$ENV* ]]; then
  11. conda activate $ENV
  12. # Check if env activation was unsuccessful, in that case create new env from file
  13. if [ $? -ne 0 ]; then
  14. echo "Conda environment '$ENV' doesn't exist. Creating it now."
  15. conda env create -q
  16. conda activate $ENV
  17. fi
  18. CONDA_ENV_ROOT="$(pwd)"
  19. fi
  20. elif [[ $PATH = */envs/* ]]\
  21. && [[ $(pwd) != $CONDA_ENV_ROOT ]]\
  22. && [[ $(pwd) != $CONDA_ENV_ROOT/* ]]
  23. then
  24. CONDA_ENV_ROOT=""
  25. conda deactivate
  26. fi
  27. done
  28. }
Add Comment
Please, Sign In to add comment