Created
March 21, 2022 17:44
-
-
Save NucciTheBoss/fdf842cca303c9159fb52a9caeb2df65 to your computer and use it in GitHub Desktop.
Move conda environments to a new location on your Linux system.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
echo "Begin environment relocation." | |
NEW_ENV_DIR=$1 | |
BASE_ENV=$(conda info --base) | |
######################################### | |
# | |
# Install jq locally | |
# | |
######################################### | |
mkdir -p ~/.local/tmp/bin | |
wget https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 -O ~/.local/tmp/bin/jq | |
chmod +x ~/.local/tmp/bin/jq | |
export PATH=~/.local/tmp/bin:$PATH | |
######################################### | |
# | |
# Relocate conda environments | |
# | |
######################################### | |
mkdir -p $NEW_ENV_DIR | |
conda config --add envs_dirs $NEW_ENV_DIR | |
for i in $(conda env list --json | jq '.envs | .[]'); do | |
i=$(sed -e 's/^"//' -e 's/"$//' <<< $i) | |
if [ $(basename $BASE_ENV) != $(basename $i) ]; then | |
conda env export -p $i > tmp.yaml | |
conda remove -p $i --all -y | |
conda create -p $NEW_ENV_DIR/$(basename $i) -y | |
sed -i "1d" tmp.yaml | |
sed -i "$(wc -l < tmp.yaml)d" tmp.yaml | |
conda env update -p $NEW_ENV_DIR/$(basename $i) -f tmp.yaml | |
fi | |
done | |
######################################### | |
# | |
# Clean up environment | |
# | |
######################################### | |
chmod -R ug+rwx $NEW_ENV_DIR | |
rm -rf ~/.local/tmp | |
rm -f tmp.yaml | |
echo "Environment relocation finished." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example command: