Skip to content

Instantly share code, notes, and snippets.

@SEJeff
Created April 9, 2011 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SEJeff/911557 to your computer and use it in GitHub Desktop.
Save SEJeff/911557 to your computer and use it in GitHub Desktop.
Shell function for automatically cd-ing into the source directory of any python module much like how you cd in the shell
# Put this in /etc/profile.d/cdpy.sh or copy the function into ~/.bashrc
# Change to the source directory of just about any python module to RTFS
#
# (c) 2011 Jeff Schroeder <jeffschroeder@computer.org> released as GPLv2
cdpy() {
module="$1"
[ -z "$module" ] && return 1
dir=$(python -c "
import os
try:
import $module
print os.path.dirname(${module}.__file__)
except ImportError:
pass
")
if [ -d "$dir" ]; then
cd "$dir"
# cd to the real directory when PYTHONPATH uses symlinks
cd $(pwd -P)
else
echo "ERROR: No directory or module $module doesn't exist" >&2
return 1
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment