Skip to content

Instantly share code, notes, and snippets.

@linxlunx
Forked from pujianto/venv_upgrade.sh
Last active December 16, 2021 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linxlunx/5e9c10873e033e665d6368e5070ba1ed to your computer and use it in GitHub Desktop.
Save linxlunx/5e9c10873e033e665d6368e5070ba1ed to your computer and use it in GitHub Desktop.
Script to rebuild virtualenv after upgrading the python version
#!/bin/bash
# This script is for rebuilding the virtualenv when using virtualenvwrapper
# Sometimes the virtualenv is broken after upgrading the python version
# Inspired from https://gist.github.com/pujianto/7df2413d64480f7797e1f0c434841283
#
# Usage:
# $ ./venv_upgrade.sh
# Input your virtualenvs base directory then select the virtualenv you want to rebuild
if [ "`command -v pip`" == "" ]; then
echo "pip is not installed" && exit
fi
# check virtualenvwrapper installed
VIRTUALENVWRAPPER_LOCATION=`command -v virtualenvwrapper.sh`
if [ "$VIRTUALENVWRAPPER_LOCATION" == "" ];
then echo "virtualenvwrapper is not installed" && exit
else eval ". $VIRTUALENVWRAPPER_LOCATION"
fi
# check venv folder config
VENV_FOLDER_CONFIG=$HOME/.venv-upgrade-config
if [ -f "$VENV_FOLDER_CONFIG" ]; then
echo "config file detected!"
read -p "Do you want to update venv folder? (y/n): " confirm_upgrade
case $confirm_upgrade in
y | Y)
read -e -p "Please input new venv folder: " new_folder
echo $new_folder > $VENV_FOLDER_CONFIG
;;
*)
echo "config file unchanged"
;;
esac
else
echo "config file not detected"
read -e -p "Please input your venv folder: " venv_folder
echo $venv_folder > $VENV_FOLDER_CONFIG
fi
echo "======================================================"
# check if the last character of venv folder is / or not
VENV_FOLDER=`cat $VENV_FOLDER_CONFIG`
if [ "${VENV_FOLDER: -1}" != "/" ];
then
VENV_FOLDER=$VENV_FOLDER"/"
fi
# select venv to reinstall
VENV_LIST=`find $VENV_FOLDER -maxdepth 1 -type d | awk -F/ '{print $5}' | sed 1d`
select venv_select in $VENV_LIST
do
VENV_SELECTED=$VENV_FOLDER$venv_select
if [ "$venv_select" != "" ];
then break
fi
done
echo "======================================================"
# get venv interpreter
VENV_INTERPRETER=`ls -l $VENV_SELECTED/bin/python | awk -F"->" '{print $2}' | rev | cut -d/ -f1 | rev`
# lib path
VENV_LIBPATH="$VENV_SELECTED/lib/$VENV_INTERPRETER/site-packages/"
# extract packages to temporary file
VENV_PACKAGE_FILE="/tmp/venv-requirements.txt"
python -m pip freeze --path $VENV_LIBPATH > $VENV_PACKAGE_FILE
# remove old venv
rmvirtualenv $venv_select
mkvirtualenv -p $VENV_INTERPRETER $venv_select
# install requirements
$VENV_SELECTED/bin/pip install -r $VENV_PACKAGE_FILE
# remove temporary file
rm $VENV_PACKAGE_FILE
echo "======================================================"
echo "Successfully rebuild virtualenv!"
echo "======================================================"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment