Created
December 15, 2010 08:42
-
-
Save MicahElliott/741781 to your computer and use it in GitHub Desktop.
Python (Version) Selector (and Activator)
This file contains hidden or 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
# Zsh (to be ‘source’d) | |
# Python (Version) Selector (and Activator) | |
# | |
# Prompts user with a menu of installed Pythons with their version | |
# string and path. Their choice results in an alias that hides any | |
# invocation of ‘python’. | |
# | |
# Potentially useful for setups with multiple ‘pip’s, | |
# ‘virtualenv[wrapper]’s, etc. | |
# | |
# Not sure yet how bad of an idea this is. | |
# | |
# | |
# % pysel | |
# q) quit | |
# 1) 2.7 /home/mde/local/bin/python | |
# 2) 2.6.5 /usr/bin/python | |
# 3) 2.6.5 /usr/bin/python2 | |
# 4) 2.6.5 /usr/bin/python2.6 | |
# 5) 2.7 /home/mde/local/bin/python2.7 | |
# 6) 3.1.2 /usr/bin/python3 | |
# 7) 3.1.2 /usr/bin/python3.1 | |
# Choose a Python to activate> 3 | |
# Activating: 2.6.5 /usr/bin/python2 | |
# | |
# Could also work for any tool you install multiples of. | |
# | |
# TODO: display the one presently active, if not first run. | |
# TODO: uniqify for dummies who have paths repeated in PATH. | |
# Maybe should also uniqify based on version numbers. | |
# TODO: Should probably sort by version number. | |
autoload -Uz colors; colors | |
pysel() { | |
local pys pypaths py vstr | |
pys=() | |
# Get most all reasonably named Pythons in PATH. | |
pypaths=( $(whence -ap python) $(whence -aps -m 'python*[0-9]') ) | |
for py in $pypaths; do | |
# Decorate them nicely in green (numbers are stupid but room for | |
# big nasty color escape sequences). | |
vstr=$( printf "%17.20s" "$fg[green]$($py -V |& awk '{print $2}')$reset_color" ) | |
pys+="$vstr $py" | |
done | |
# Menu. | |
PROMPT3="$fg[yellow]Choose a Python to activate>$reset_color " | |
print "q) $fg[red]quit$reset_color" | |
select py in $pys; do | |
if [[ "$REPLY" = q ]]; then | |
break | |
elif [[ -n "$py" ]]; then | |
print "Activating: $py" | |
alias python="$(echo $py |awk '{print $2}')" | |
break | |
fi | |
done | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment