Skip to content

Instantly share code, notes, and snippets.

@JonathonReinhart
Created August 18, 2020 01:07
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save JonathonReinhart/4f332b18b828eb1222767d44caa41ad0 to your computer and use it in GitHub Desktop.
Bash function for easy virtualenv activation
# Put this in your .bashrc or .bash_aliases
# List virtual envs
function list-virtualenvs() {
for d in *; do
if [[ -d "${d}" && -f "${d}/bin/activate" ]]; then
echo "${d}"
fi
done
}
# Easy virtualenv activation
function ve() {
if [[ $# -eq 1 ]]; then
venv="$1"
elif [[ $# -eq 0 ]]; then
venvs=($(list-virtualenvs))
# How many virtualenvs are available?
case ${#venvs[@]} in
0)
echo "No virtualenvs found"
;;
1)
venv="${venvs[0]}"
;;
*)
echo "Available virtualenvs (${#venvs[@]}):"
for v in "${venvs[@]}"; do
echo " $v"
done
false
return
;;
esac
fi
if [[ -z $venv ]]; then
echo "Usage: ve virtualenv-dir"
false
return
fi
act="${venv}/bin/activate"
if [[ ! -f $act ]]; then
echo "ve: \"$venv\" does not appear to be a virtualenv"
false
return
fi
echo "Activating virtualenv: ${venv}"
source $act
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment