An easy way to do this is to go beyond the interpreter and just find documents .
Meanwhile, it seems to me that there is no such function for import. There are group_by methods for sqlalchemy.orm.query.Query and sqlalchemy.sql.expression.[Compound]Select[Base] objects.
But if you really want to recursively go through all the modules in the package looking for a name, here is how you do it:
import inspect def find_name(package, name): if hasattr(package, name): yield package for modulename, submodule in inspect.getmembers(package, inspect.ismodule): yield from find_name(submodule, name)
For Python 3.2 or earlier, you need to replace the yield from loop with a loop:
for modulename, submodule in inspect.getmembers(package, inspect.ismodule): for result in find_name(submodule, name): yield result
And if you just want to get the first result, instead of all the results, you can simply return instead of yield ing:
def find_name(package, name): if hasattr(package, name): return package for modulename, submodule in inspect.getmembers(package, inspect.ismodule): result = find_name(submodule, name) if result: return result
source share