An iterator for the modules that are imported by the specified *module*
    or Python source file. The returned #ModuleInfo objects have their
    *imported_from* member filled in order to be able to track how a module
    was imported.

def iter_modules(self, module=None, filename=None, source=None, excludes=None):
    """
    An iterator for the modules that are imported by the specified *module*
    or Python source file. The returned #ModuleInfo objects have their
    *imported_from* member filled in order to be able to track how a module
    was imported.
    """

    if excludes is None:
      excludes = self.excludes

    if not filename:
      if not module:
        raise ValueError('need either module or filename parameter')
      module = self.find_module(module)
      if not module.filename or module.type == 'native':
        return
    else:
      module = ModuleInfo('__main__', filename, ModuleInfo.SRC)

    seen = set()
    stack = collections.deque()

    for imp in get_imports(module.filename, source):
      stack.appendleft((module.join_import_from(imp.name), [module.name]))

    yield module
    while stack:
      import_name, imported_from = stack.pop()
      if import_name in seen:
        continue
      seen.add(import_name)
      if check_module_exclude(import_name, excludes):
        continue

      module = self.find_module(import_name)
      module.imported_from[:] = imported_from
      yield module

      if module.type == ModuleInfo.SRC:
        imported_from = [module.name] + imported_from
        for imp in get_imports(module.filename):
          stack.append((module.join_import_from(imp.name), imported_from))