import importlib


def _import_name(generator):
    """Import a class from its module name."""
    # A generator 'path'
    try:
        mod_name, func_name = generator.rsplit(".", 1)
    except ValueError:
        raise ValueError("Invalid generator class name %s" % generator)

    try:
        mod = importlib.import_module(mod_name)
    except ImportError as e:
        raise ValueError("Error importing generator module %s: '%s'" % (mod_name, e))

    try:
        return getattr(mod, func_name)
    except AttributeError:
        raise ValueError(
            "Module '%s' does not define a '%s' class" % (mod_name, func_name)
        )