Skip to content

Instantly share code, notes, and snippets.

@svenXY
Last active April 26, 2024 07:37
Show Gist options
  • Save svenXY/5edd0906c736d217b0121f44e7dee9b8 to your computer and use it in GitHub Desktop.
Save svenXY/5edd0906c736d217b0121f44e7dee9b8 to your computer and use it in GitHub Desktop.
Quick & dirty script to migrate pyenvs from one python version to a new one (e.g. 3.11.1 to 3.12.2). Old virtualenvs are first deleted in the process!
old=$1
new=$2
if [[ -z "$old" || -z "$new" ]]; then
echo "usage upgrade_pyenvs.zsh <old_version> <new_version>"
exit 1
fi
for myenv in $(pyenv versions --bare | rg $old/envs); do
myenv=${myenv##$old/envs/}
echo "Upgrading $myenv"
pyenv shell $myenv
python -m pip freeze > requirements_$old_${myenv}.txt
python -m pip freeze | cut -d"=" -f1 > requirements_${myenv}_names.txt
### !!!! this will first delete the "old" virtualenv, since renaming is not possible!
pyenv virtualenv-delete $myenv
if [[ $? -ne 0 ]]; then
echo "Failed to delete or chose not to delete $myenv"
continue
fi
pyenv virtualenv $new $myenv
pyenv shell $myenv
# this might throw errors that you will have to fix afterwards
python -m pip install -r requirements_${myenv}_names.txt
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment