Created
April 9, 2011 17:05
-
-
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
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 /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