Skip to content

Instantly share code, notes, and snippets.

@pavoljuhas
Last active February 6, 2018 16:04
Show Gist options
  • Save pavoljuhas/8657001 to your computer and use it in GitHub Desktop.
Save pavoljuhas/8657001 to your computer and use it in GitHub Desktop.
pywhichmodule, pypath -- find out what Python imports and where does it look
#!/usr/bin/env python
import sys
import os
mydir = os.path.realpath(os.path.dirname(sys.argv[0]))
notmydir = lambda d : not (os.path.exists(d) and
os.path.realpath(d) == mydir)
p = filter(notmydir, sys.path)
print('\n'.join(p))
#!/bin/sh
# vim: ft=zsh
if test -z "$ZSH_VERSION"; then
# Mac OS X 10.7 (Lion) is misconfigured to reset PATH for "zsh -f"
# Save the original value so it can be restored in zsh.
export PATH_NwldpJ="$PATH"
exec /bin/zsh -f "$0" "$@"
fi
# restore the PATH in zsh
PATH=${PATH_NwldpJ}
unset PATH_NwldpJ
# avoid overrides from environment variables
unset -m 'opt_*|my_*'
# running in zsh -------------------------------------------------------------
my_doc='pywhichmodule print full path to specifed python module(s)
usage: pywhichmodule [options] mod1 [mod2 ...]
Options:
-g change to /tmp to ignore any local python files
-v import and show module __version__
--python=PYTHON Python executable to be used for module lookup
-h, --help display this message and exit
'
# default value for the --python option
opt_python=( --python python )
# parse command line options
zmodload zsh/zutil
zparseopts -K -E -D \
h=opt_help -help=opt_help v=opt_v g=opt_g -python:=opt_python || exit $?
if [[ -n $opt_help ]]; then
print ${my_doc%[[:space:]]}
exit
fi
if [[ -n $opt_g ]]; then
cd /tmp
fi
my_python=${opt_python[2]#=}
my_cmd='import @m@ as mod; print(mod.__file__)'
if [[ -n $opt_v ]]; then
my_fv="mod.__file__ + ' ' + mod.__version__"
my_cmd=${my_cmd/mod.__file__/$my_fv}
fi
for m; do
${my_python} -c ${my_cmd//@m@/$m} || my_ec=$?
done
exit $my_ec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment