Skip to content

Instantly share code, notes, and snippets.

@eestrada
Created November 4, 2015 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eestrada/e2a015f6e1389da81543 to your computer and use it in GitHub Desktop.
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.
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