Skip to content

Instantly share code, notes, and snippets.

@ernstki
Last active February 19, 2022 02:14
Show Gist options
  • Save ernstki/a43d4c875c008e197027b4aff056ca6b to your computer and use it in GitHub Desktop.
Save ernstki/a43d4c875c008e197027b4aff056ca6b to your computer and use it in GitHub Desktop.
pywhich - tell me where a Python library came from
#!/usr/bin/env python
"""
pywhich - tell me where a Python package came from
Original author: Adam Anderson
Source: https://stackoverflow.com/a/42365058
License: CC-BY-SA-4.0
"""
from __future__ import print_function
GIST = 'https://gist.github.com/ernstki/a43d4c875c008e197027b4aff056ca6b'
def pywhich(module_name):
module = __import__(module_name, globals(), locals(), [], 0)
return module.__file__
if __name__ == "__main__":
import sys
import argparse
parser = argparse.ArgumentParser(
description="print where a Python package comes from",
epilog="Source: {}".format(GIST))
parser.add_argument('pkgs', metavar='PKG', nargs='+',
help='package(s) to look for')
opts = parser.parse_args()
for pkg in opts.pkgs:
try:
print(pywhich(pkg))
except ImportError:
parser.error("No such module '{}'.".format(pkg))
@ernstki
Copy link
Author

ernstki commented Feb 19, 2022

Example:

$ pywhich tkinter
/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment