Skip to content

Instantly share code, notes, and snippets.

@airvzxf
Created July 2, 2025 16:26
Show Gist options
  • Select an option

  • Save airvzxf/50b493e6583a5288c7a6cbb51c546876 to your computer and use it in GitHub Desktop.

Select an option

Save airvzxf/50b493e6583a5288c7a6cbb51c546876 to your computer and use it in GitHub Desktop.
Module providing a function displaying the sub-packages from a package.
"""Module providing a function displaying the sub-packages from a package."""
import pkgutil
import sys
def list_subpackages_pkgutil(package) -> list:
"""
List all sub-packages of a given package..
"""
package_path = package.__path__
package_prefix = package.__name__ + "."
print(f"Sub-packages of: '{package.__name__}'")
modules: list = []
for _, package_name, _ in pkgutil.walk_packages(package_path, package_prefix):
modules.append(package_name)
modules.sort()
return modules
def list_subpackages_ram(package) -> list:
"""
List the sub-packages of an already imported package.
"""
package_prefix = package.__name__ + "."
print(f"Sub-packages in sys.modules of: '{package.__name__}")
modules: list = []
for module in sys.modules.items():
package_name = module[0]
if package_name.startswith(package_prefix):
modules.append(package_name)
modules.sort()
return modules
def list_subpackages(pakcage) -> list:
"""
List the sub-packages of an unique imported package.
"""
modules_pkgutil = list_subpackages_pkgutil(pakcage)
modules_ram = list_subpackages_ram(pakcage)
modules_combined = list(set(modules_pkgutil + modules_ram))
modules_combined.sort()
return modules_combined
if __name__ == "__main__":
import google.adk
modules_unique = list_subpackages(google.adk)
for module_unique in modules_unique:
print(module_unique)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment