import importlib
import pkgutil


def list_submodules(package, recursive=True):
    """
    Recursively (optional) find the submodules from a module or directory

    Args:
        package (str or module): Root module or directory to load submodules from
        recursive (bool, optional): Recursively find. Defaults to True.

    Returns:
        array: array containing module paths that can be imported
    """
    if isinstance(package, str):
        package = importlib.import_module(package)
    results = []
    for _loader, name, is_pkg in pkgutil.walk_packages(package.__path__):
        full_name = package.__name__ + '.' + name
        results.append(full_name)
        if recursive and is_pkg:
            results.extend(list_submodules(full_name))
    return results