Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
act-venv () {
local ACTIVATION_SCRIPT
local VENV_DIRS
ACTIVATION_SCRIPT="bin/activate"
VENV_DIRS=("env" "venv" "python-venv")
local dir="$(pwd)"
local venv=false
local debug=false
[ "$1" = "-v" ] && debug=true
while [ "${dir}" != "/" ]
do
for venv_dir in ${VENV_DIRS[@]}
do
local activation="${dir}/${venv_dir}/${ACTIVATION_SCRIPT}"
[ ${debug} = true ] && echo "searching for ${activation}..."
if [ -f "${activation}" ]
then
if [ "${VIRTUAL_ENV}" != "" ]
then
echo "Python virtual environment \"${VIRTUAL_ENV}\" will be deactivated."
deactivate
fi
source ${activation}
echo "Python virtual environment has been set to \"${VIRTUAL_ENV}\"."
venv=true
break
fi
done
[ ${venv} = true ] && break
dir=$(dirname ${dir})
done
if [ ${venv} = false ]
then
echo "Python virtual enviroment was not found." >&2
return 1
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment