from importlib import import_module


def try_import_for_setup(module_name):
    """
    Returns the imported module named :samp:`{module_name}`.
    Attempts to import the module named :samp:`{module_name}` and
    translates any raised :obj:`ImportError` into a :obj:`NotImplementedError`.
    This is useful in benchmark :samp:`setup`, so that a failed import results in
    the benchmark getting *skipped*.

    :type module_name: :obj:`str`
    :param module_name: Attempt to import this module.
    :rtype: :obj:`module`
    :returns: The imported module named :samp:`{module_name}`.
    :raises NotImplementedError: if there is an :obj:`ImportError`.
    """
    try:
        module = import_module(module_name)
    except ImportError as e:
        raise NotImplementedError("Error during '%s' module import: %s." % (module_name, str(e)))

    return module