9

I know how to export a specific conda environment:

conda activate myenv
conda env export > myenv.yaml

But how can I automatically export all created conda environments (in separate yaml files, whose name corresponds to the name of the environment)?

Tom de Geus
  • 5,625
  • 2
  • 33
  • 77

2 Answers2

18

You don't need to activate the environment. conda env export accepts the argument -n <env name> which you can combine with a for loop over the output of conda list:

for env in $(conda env list | cut -d" " -f1); do 
   if [[ ${env:0:1} == "#" ]] ; then continue; fi;
   conda env export -n $env > ${env}.yml
done
FlyingTeller
  • 17,638
  • 3
  • 38
  • 53
  • Awesome! Thanks a lot. I guess the only a bit dirty hack is the `tail -n+4`. In particular, I only get two lines of comment. So a bit more general would be to remove the `tail` and add `if [[ ${env:0:1} == "#" ]] ; then continue; fi;` in the for-loop. – Tom de Geus Mar 04 '20 at 08:07
  • 1
    Thanks, I put in the recommendation. Feel free to edit, if you see an improvement :) – FlyingTeller Mar 04 '20 at 10:44
  • Shouldn't it be `conda env export -n $env > ${env}.yml`? – Helder Oct 22 '20 at 18:31
0
conda env list > ToENV
sed -i '1,4d' ToENV # remove first 4 lines
awk  '{ print " conda pack -n "$1 " -o "$1}' ToENV | sed 's~$~.tar.gz~g' > BACK.sh

Open BACK.sh and review and remove those line you don't need.

bash BACK.sh
Andrés Parada
  • 319
  • 7
  • 21