Created
August 20, 2014 13:08
-
-
Save arne-cl/03aec5cfb4bc0bdb0528 to your computer and use it in GitHub Desktop.
prints the install path of a given Python package
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# Author: Arne Neumann | |
# | |
# Purpose: prints the path where the given Python package is installed. | |
# This might be interesting if you're working with multiple environments | |
# and are unsure if/where a package was installed. | |
import os | |
import sys | |
def which(package): | |
try: | |
exec("import {}".format(package)) | |
except ImportError as e: | |
return "{}. Did you install {} in this environment?".format(e, package) | |
try: | |
return os.path.abspath(os.path.dirname(eval(package).__file__)) | |
except AttributeError as e: | |
return "{}. {} is probably a built-in module.".format(e, package) | |
if __name__ == '__main__': | |
if len(sys.argv) >= 2: | |
for i, package in enumerate(sys.argv[1:], 1): | |
print which(sys.argv[i]) | |
else: | |
print "{} prints the path where the given Python packages are installed.".format(sys.argv[0]) | |
print "{} PYTHON_PACKAGE_NAME".format(sys.argv[0]) | |
sys.exit(1) |
There's prior art:
pwhich 0.1.0 6 Python which - find out where Python modules are located on your drive
pywhich 1.1.1 5 Find where python modules are installed.
whichpkg 0.4.0 5 Locate the path of a specific python module
Also see http://stackoverflow.com/questions/247770/retrieving-python-module-path/12154601#12154601
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO: sanitize input (e.g. validate that
package
is a valid package nameand not the Python equivalent of ;DROP TABLE)