Created
August 18, 2020 01:07
Star
You must be signed in to star a gist
Bash function for easy virtualenv activation
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
# 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