def tryimport(modname, pipiname=None, ensure=False):
    """
    CommandLine:
        python -m utool.util_import --test-tryimport

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_tests import *  # NOQA
        >>> import utool as ut
        >>> modname = 'pyfiglet'
        >>> pipiname = 'git+https://github.com/pwaller/pyfiglet'
        >>> pyfiglet = ut.tryimport(modname, pipiname)
        >>> assert pyfiglet is None or isinstance(pyfiglet, types.ModuleType), 'unknown error'

    Example2:
        >>> # UNSTABLE_DOCTEST
        >>> # disabled because not everyone has access to being a super user
        >>> from utool.util_tests import *  # NOQA
        >>> import utool as ut
        >>> modname = 'lru'
        >>> pipiname = 'git+https://github.com/amitdev/lru-dict'
        >>> lru = ut.tryimport(modname, pipiname, ensure=True)
        >>> assert isinstance(lru, types.ModuleType), 'did not ensure lru'
    """
    if pipiname is None:
        pipiname = modname
    try:
        if util_inject.PRINT_INJECT_ORDER:
            if modname not in sys.modules:
                util_inject.noinject(modname, N=2, via='ut.tryimport')
        module = __import__(modname)
        return module
    except ImportError as ex:
        import utool as ut
        base_pipcmd = 'pip install %s' % pipiname
        sudo  = not ut.WIN32 and not ut.in_virtual_env()
        if sudo:
            pipcmd = 'sudo ' + base_pipcmd
        else:
            pipcmd = base_pipcmd
        msg = 'unable to find module %s. Please install: %s' % ((modname), (pipcmd))
        print(msg)
        ut.printex(ex, msg, iswarning=True)
        if ensure:
            raise AssertionError('Ensure is dangerous behavior and is is no longer supported.')
            #raise NotImplementedError('not ensuring')
            ut.cmd(base_pipcmd, sudo=sudo)
            module = tryimport(modname, pipiname, ensure=False)
            if module is None:
                raise AssertionError('Cannot ensure modname=%r please install using %r'  % (modname, pipcmd))
            return module
        return None
def tryimport ( modname , pipiname = None , ensure = False ) : if pipiname is None : pipiname = modname try : if util_inject . PRINT_INJECT_ORDER : if modname not in sys . modules : util_inject . noinject ( modname , N = 2 , via = 'ut.tryimport' ) module = __import__ ( modname ) return module except ImportError as ex : import utool as ut base_pipcmd = 'pip install %s' % pipiname sudo = not ut . WIN32 and not ut . in_virtual_env ( ) if sudo : pipcmd = 'sudo ' + base_pipcmd else : pipcmd = base_pipcmd msg = 'unable to find module %s. Please install: %s' % ( ( modname ) , ( pipcmd ) ) print ( msg ) ut . printex ( ex , msg , iswarning = True ) if ensure : raise AssertionError ( 'Ensure is dangerous behavior and is is no longer supported.' ) ut . cmd ( base_pipcmd , sudo = sudo ) module = tryimport ( modname , pipiname , ensure = False ) if module is None : raise AssertionError ( 'Cannot ensure modname=%r please install using %r' % ( modname , pipcmd ) ) return module return None
CommandLine:
        python -m utool.util_import --test-tryimport

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_tests import *  # NOQA
        >>> import utool as ut
        >>> modname = 'pyfiglet'
        >>> pipiname = 'git+https://github.com/pwaller/pyfiglet'
        >>> pyfiglet = ut.tryimport(modname, pipiname)
        >>> assert pyfiglet is None or isinstance(pyfiglet, types.ModuleType), 'unknown error'

    Example2:
        >>> # UNSTABLE_DOCTEST
        >>> # disabled because not everyone has access to being a super user
        >>> from utool.util_tests import *  # NOQA
        >>> import utool as ut
        >>> modname = 'lru'
        >>> pipiname = 'git+https://github.com/amitdev/lru-dict'
        >>> lru = ut.tryimport(modname, pipiname, ensure=True)
        >>> assert isinstance(lru, types.ModuleType), 'did not ensure lru'