Skip to content

Instantly share code, notes, and snippets.

@jphalip
Created July 10, 2013 16:10
Show Gist options
  • Save jphalip/5967635 to your computer and use it in GitHub Desktop.
Save jphalip/5967635 to your computer and use it in GitHub Desktop.
Bash & zshell script for quickly accessing Python packages installed in the current virtualenv. Allows for tab completion. Associated blog post: http://julienphalip.com/post/55092823910/a-script-to-quickly-access-the-source-of-installed
# Author: Julien Phalip
# License: BSD
# Description: Change the current directory to the path of the given Python package.
function goto {
cd `python -c "import pkgutil; print(pkgutil.get_loader('$1').filename)"`
}
function _top_level_packages {
python -c "import pkgutil; print('\n'.join([name for loader, name, ispkg in sorted(pkgutil.iter_modules()) if ispkg]))"
}
if [ -n "$BASH" ] ; then
_goto_complete () {
local cur="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=( $(compgen -W "`_top_level_packages`" -- ${cur}) )
}
complete -o default -o nospace -F _goto_complete goto
elif [ -n "$ZSH_VERSION" ] ; then
_goto_complete () {
reply=( $(_top_level_packages) )
}
compctl -K _goto_complete goto
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment