backup all conda envs all at once and uninstall conda

bash script to backup all conda environment lists

#!bin/bash

NOW=$(date "+%Y-%m-%d")
CONDA_BASE=$(conda info --base)
CONDA_FUNCTION="etc/profile.d/conda.sh"
CONDA="$CONDA_BASE/$CONDA_FUNCTION"
source $CONDA

mkdir ./condaenvs-$NOW

ENVS=$(conda env list | grep '^\w' | cut -d' ' -f1)
for env in $ENVS; do
    conda activate $env
    conda env export > ./condaenvs-$NOW/$env.yml
    echo "Exporting $env"
done

python to backup all conda environment lists

import subprocess as sub
import pandas as pd

# create list of current environments

sub.check_call(" ".join(['conda','env','list','>','envs.txt']),shell=True)

# load and parse environment names

envs = pd.read_csv("envs.txt",header=1)
env_names = envs['#'].str.split(expand=True)[0]

# write environment packages out

for env in env_names:
    print("Backing up...",env)
    cmd = "conda env export --name %s > %s.yml" % (env,env)
    sub.check_call(cmd,shell=True)

uninstall anaconda

  1. rm -rf ~/anaconda3
  2. conda uninstall
  3. rm -rf ~/anaconda
  4. Edit ~/.bash_profileand remove the anaconda directory from your PATH environment variable.
  5. edit .bashrc or .profile or .bash_profile. Remove the following hidden files and directories in the home directory:.condarcand .conda

Create all envs from previous backded-up envs :

filenames=`ls *.yml`
for entry in $filenames; do
    echo $entry
    conda env create -f $entry
done

Note: sudo rm ./local/bin/jupyter*

When uninstalling and installing packages in the conda python environment, I encountered problem that the jupyter notebook cannot open and jupyter notebook is searching for previous conda env binarys.

This can be easily solved by editing the files in .local/bin (which is by default in the PATH variable to search for). In my case: in the .local/bin directory I find many jupyter ‘cache’ files that the first line is saying using previous conda python environment (not global python environment), Id deleted these files and it’s good!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值