Created
November 4, 2015 00:28
-
-
Save eestrada/e2a015f6e1389da81543 to your computer and use it in GitHub Desktop.
Find all modules of a given name on the Python path. This is similar to running `which -a <cmd>` on the Linux Bash 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
from __future__ import print_function | |
import sys | |
import imp | |
def find_all(modname, paths=None): | |
"""Find all occurrences of a given module on a given list of paths. | |
If no paths are given, sys.path is used. | |
""" | |
paths = paths or sys.path | |
for path in paths: | |
try: | |
info = imp.find_module(modname, [path]) | |
except ImportError: | |
pass | |
else: | |
mod = imp.load_module(modname, *info) | |
yield mod, path, info | |
if __name__ == '__main__': | |
for info in find_all('PySide'): | |
print(info) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment