import importlib


def _import_component(component_spec: str, force_component: bool = False):
    """
    Import a module or module component from *spec*.

    :param component_spec: String of the form "module_name" or "module_name:component_name" where
        module_name must an absolute, fully qualified path to a module.
    :param force_component: If True, *spec* must specify a component name
    :return: the imported module or module component
    """
    if ':' in component_spec:
        module_name, component_name = component_spec.split(':', maxsplit=1)
    else:
        module_name, component_name = component_spec, None

    if force_component and component_name is None:
        raise ValueError('illegal spec, must specify a component')
    if module_name == '':
        raise ValueError('illegal spec, missing module path')
    if component_name == '':
        raise ValueError('illegal spec, missing component name')

    module = importlib.import_module(module_name)
    return getattr(module, component_name) if component_name else module