"""Contains all benchmark imports and a list of benchmarks."""

import pkgutil


def _LoadModules():
  result = []
  for importer, modname, ispkg in pkgutil.iter_modules(__path__):
    result.append(importer.find_module(modname).load_module(modname))
  return result


BENCHMARKS = _LoadModules()


import importlib
import pkgutil


def _LoadModulesForPath(path, package_prefix=None):
  """Load all modules on 'path', with prefix 'package_prefix'.

  Example usage:
    _LoadModulesForPath(__path__, __name__)

  Args:
    path: Path containing python modules.
    package_prefix: prefix (e.g., package name) to prefix all modules.
      'path' and 'package_prefix' will be joined with a '.'.
  Yields:
    Imported modules.
  """
  prefix = ''
  if package_prefix:
    prefix = package_prefix + '.'
  module_iter = pkgutil.iter_modules(path, prefix=prefix)
  for _, modname, ispkg in module_iter:
    if not ispkg:
      yield importlib.import_module(modname)


def _LoadBenchmarks():
  return list(_LoadModulesForPath(__path__, __name__))


BENCHMARKS = _LoadBenchmarks()

Fix a bug in dynamic benchmark loading.
Fix a bug in dynamic benchmark loading.

perfkitbenchmarker/benchmarks/__init__.py used ImpImporter.load_module
to import benchmarks, which caused an error when they were later
imported directly by an import statement. Switched to 'importlib' to resolve.

- """Contains all benchmark imports and a list of benchmarks."""
  
+ import importlib
  import pkgutil
  
  
- def _LoadModules():
-   result = []
-   for importer, modname, ispkg in pkgutil.iter_modules(__path__):
-     result.append(importer.find_module(modname).load_module(modname))
-   return result
+ def _LoadModulesForPath(path, package_prefix=None):
+   """Load all modules on 'path', with prefix 'package_prefix'.
+ 
+   Example usage:
+     _LoadModulesForPath(__path__, __name__)
+ 
+   Args:
+     path: Path containing python modules.
+     package_prefix: prefix (e.g., package name) to prefix all modules.
+       'path' and 'package_prefix' will be joined with a '.'.
+   Yields:
+     Imported modules.
+   """
+   prefix = ''
+   if package_prefix:
+     prefix = package_prefix + '.'
+   module_iter = pkgutil.iter_modules(path, prefix=prefix)
+   for _, modname, ispkg in module_iter:
+     if not ispkg:
+       yield importlib.import_module(modname)
  
  
- BENCHMARKS = _LoadModules()
+ def _LoadBenchmarks():
+   return list(_LoadModulesForPath(__path__, __name__))
  
+ 
+ BENCHMARKS = _LoadBenchmarks()
+ 
Fix a bug in dynamic benchmark loading.
## Code Before:
"""Contains all benchmark imports and a list of benchmarks."""

import pkgutil


def _LoadModules():
  result = []
  for importer, modname, ispkg in pkgutil.iter_modules(__path__):
    result.append(importer.find_module(modname).load_module(modname))
  return result


BENCHMARKS = _LoadModules()

## Instruction:
Fix a bug in dynamic benchmark loading.
## Code After:

import importlib
import pkgutil


def _LoadModulesForPath(path, package_prefix=None):
  """Load all modules on 'path', with prefix 'package_prefix'.

  Example usage:
    _LoadModulesForPath(__path__, __name__)

  Args:
    path: Path containing python modules.
    package_prefix: prefix (e.g., package name) to prefix all modules.
      'path' and 'package_prefix' will be joined with a '.'.
  Yields:
    Imported modules.
  """
  prefix = ''
  if package_prefix:
    prefix = package_prefix + '.'
  module_iter = pkgutil.iter_modules(path, prefix=prefix)
  for _, modname, ispkg in module_iter:
    if not ispkg:
      yield importlib.import_module(modname)


def _LoadBenchmarks():
  return list(_LoadModulesForPath(__path__, __name__))


BENCHMARKS = _LoadBenchmarks()
