import_tools

Functions for importing classes.

New in version 0.5.0.

Functions:

discover(package[, match_func, …])

Returns a list of objects in the given package, optionally filtered by match_func.

discover_entry_points(group_name[, match_func])

Returns a list of entry points in the given category, optionally filtered by match_func.

discover_entry_points_by_name(group_name[, …])

Returns a mapping of entry point names to the entry points in the given category, optionally filtered by name_match_func and object_match_func.

discover_in_module(module[, match_func, …])

Returns a list of objects in the given module, optionally filtered by match_func.

iter_submodules(module)

Returns an iterator over the names of the submodules and subpackages of the given module.

discover(package, match_func=None, exclude_side_effects=True)[source]

Returns a list of objects in the given package, optionally filtered by match_func.

Parameters
  • package (ModuleType) – A Python package

  • match_func (Optional[Callable[[Any], bool]]) – Function taking an object and returning True if the object is to be included in the output. Default None, which includes all objects.

  • exclude_side_effects (bool) – Don’t include objects that are only there because of an import side effect. Default True.

Return type

List[Any]

Overloads

Changed in version 1.0.0: Added the exclude_side_effects parameter.

discover_entry_points(group_name, match_func=None)[source]

Returns a list of entry points in the given category, optionally filtered by match_func.

New in version 1.1.0.

Parameters
  • group_name (str) – The entry point group name, e.g. 'entry_points'.

  • match_func (Optional[Callable[[Any], bool]]) – Function taking an object and returning True if the object is to be included in the output. Default None, which includes all objects.

Return type

List[Any]

Returns

List of matching objects.

discover_entry_points_by_name(group_name, name_match_func=None, object_match_func=None)[source]

Returns a mapping of entry point names to the entry points in the given category, optionally filtered by name_match_func and object_match_func.

New in version 2.5.0.

Parameters
  • group_name (str) – The entry point group name, e.g. 'entry_points'.

  • name_match_func (Optional[Callable[[Any], bool]]) – Function taking the entry point name and returning True if the entry point is to be included in the output. Default None, which includes all entry points.

  • object_match_func (Optional[Callable[[Any], bool]]) – Function taking an object and returning True if the object is to be included in the output. Default None, which includes all objects.

Return type

Dict[str, Any]

discover_in_module(module, match_func=None, exclude_side_effects=True)[source]

Returns a list of objects in the given module, optionally filtered by match_func.

New in version 2.6.0.

Parameters
  • module (ModuleType) – A Python module.

  • match_func (Optional[Callable[[Any], bool]]) – Function taking an object and returning True if the object is to be included in the output. Default None, which includes all objects.

  • exclude_side_effects (bool) – Don’t include objects that are only there because of an import side effect. Default True.

Return type

List[Any]

iter_submodules(module)[source]

Returns an iterator over the names of the submodules and subpackages of the given module.

New in version 2.6.0.

Parameters

module (str)

Return type

Iterator[str]