Check if a python module is installed without attempting to import it.
    Note, that if ``modname`` indicates a child module, the parent module is
    always loaded.

    Args:
        modname (str):  module name

    Returns:
        bool: found

    References:
        http://stackoverflow.com/questions/14050281/module-exists-without-importing

    CommandLine:
        python -m utool.util_import check_module_installed --show --verbimp --modname=this
        python -m utool.util_import check_module_installed --show --verbimp --modname=guitool
        python -m utool.util_import check_module_installed --show --verbimp --modname=guitool.__PYQT__
        python -m utool.util_import check_module_installed --show --verbimp --modname=ibeis.scripts.iccv

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_import import *  # NOQA
        >>> import utool as ut
        >>> modname = ut.get_argval('--modname', default='this')
        >>> is_installed = check_module_installed(modname)
        >>> is_imported = modname in sys.modules
        >>> print('module(%r).is_installed = %r' % (modname, is_installed))
        >>> print('module(%r).is_imported = %r' % (modname, is_imported))
        >>> assert 'this' not in sys.modules, 'module(this) should not have ever been imported'

def check_module_installed(modname):
    """
    Check if a python module is installed without attempting to import it.
    Note, that if ``modname`` indicates a child module, the parent module is
    always loaded.

    Args:
        modname (str):  module name

    Returns:
        bool: found

    References:
        http://stackoverflow.com/questions/14050281/module-exists-without-importing

    CommandLine:
        python -m utool.util_import check_module_installed --show --verbimp --modname=this
        python -m utool.util_import check_module_installed --show --verbimp --modname=guitool
        python -m utool.util_import check_module_installed --show --verbimp --modname=guitool.__PYQT__
        python -m utool.util_import check_module_installed --show --verbimp --modname=ibeis.scripts.iccv

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_import import *  # NOQA
        >>> import utool as ut
        >>> modname = ut.get_argval('--modname', default='this')
        >>> is_installed = check_module_installed(modname)
        >>> is_imported = modname in sys.modules
        >>> print('module(%r).is_installed = %r' % (modname, is_installed))
        >>> print('module(%r).is_imported = %r' % (modname, is_imported))
        >>> assert 'this' not in sys.modules, 'module(this) should not have ever been imported'
    """
    import pkgutil
    if '.' in modname:
        # Prevent explicit import if possible
        parts = modname.split('.')
        base = parts[0]
        submods = parts[1:]
        loader = pkgutil.find_loader(base)
        if loader is not None:
            # TODO: check to see if path to the submod exists
            submods
            return True
    loader = pkgutil.find_loader(modname)
    is_installed = loader is not None
    return is_installed