Exemple #1
0
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
Exemple #2
0
def import_modname(modname):
    r"""
    Args:
        modname (str):  module name

    Returns:
        module: module

    CommandLine:
        python -m utool.util_import --test-import_modname

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_import import *  # NOQA
        >>> modname_list = [
        >>>     'utool',
        >>>     'utool._internal',
        >>>     'utool._internal.meta_util_six',
        >>>     'utool.util_path',
        >>>     #'utool.util_path.checkpath',
        >>> ]
        >>> modules = [import_modname(modname) for modname in modname_list]
        >>> result = ([m.__name__ for m in modules])
        >>> assert result == modname_list
    """
    # The __import__ statment is weird
    if util_inject.PRINT_INJECT_ORDER:
        if modname not in sys.modules:
            util_inject.noinject(modname, N=2, via='ut.import_modname')
    if '.' in modname:
        fromlist = modname.split('.')[-1]
        fromlist_ = list(map(str, fromlist))  # needs to be ascii for python2.7
        module = __import__(modname, {}, {}, fromlist_, 0)
    else:
        module = __import__(modname, {}, {}, [], 0)
    return module
Exemple #3
0
    DynStruct probably needs to go away.
"""
from __future__ import absolute_import, division, print_function, unicode_literals
import six
from six.moves import cPickle as pickle
try:
    import numpy as np
except ImportError as ex:
    pass
from utool import DynamicStruct
from utool import util_dbg
from utool import util_arg
from utool import util_type
from utool import util_inject
# print, rrr, profile = util_inject.inject(__name__, '[pref]')
util_inject.noinject(__name__, '[pref]')

# ---
# GLOBALS
# ---
PrefNode = DynamicStruct.DynStruct


VERBOSE_PREF = util_arg.get_argflag('--verbpref')


# ---
# Classes
# ---
class PrefInternal(DynamicStruct.DynStruct):
    def __init__(_intern, name, doc, default, hidden, fpath, depeq, choices):
Exemple #4
0
import threading
from six.moves import map, range, zip  # NOQA
from utool._internal.meta_util_six import get_funcname
from utool import util_progress
from utool import util_time
from utool import util_arg
from utool import util_dbg
from utool import util_inject
from utool import util_cplat
if six.PY2:
    import thread as _thread
    import Queue as queue
elif six.PY3:
    import _thread
    import queue
util_inject.noinject('[parallel]')


FUTURE_ON = False
QUIET   = util_arg.QUIET
SILENT  = util_arg.SILENT
VERBOSE_PARALLEL, VERYVERBOSE_PARALLEL = util_arg.get_module_verbosity_flags('par', 'parallel')
#VERBOSE_PARALLEL = util_arg.VERBOSE or util_arg.get_argflag(('--verbose-par', '--verbpar', '--verbose-parallel', '--verbparallel'))
#VERYVERBOSE_PARALLEL = util_arg.VERYVERBOSE or util_arg.get_argflag(('--veryverbose-par', '--veryverbpar', '--veryverbose-parallel', '--veryverbparallel'))
STRICT  = util_arg.STRICT

if SILENT:
    def print(msg):
        pass

__POOL__ = None
Exemple #5
0
def import_module_from_fpath(module_fpath):
    r""" imports module from a file path

    Args:
        module_fpath (str):

    Returns:
        module: module

    CommandLine:
        python -m utool.util_import --test-import_module_from_fpath

    Example:
        >>> # DISABLE_DOCTEST
        >>> from utool.util_import import *  # NOQA
        >>> module_fpath = '?'
        >>> module = import_module_from_fpath(module_fpath)
        >>> result = ('module = %s' % (str(module),))
        >>> print(result)


    Ignore:
        module_fpath = '/home/joncrall/code/h5py/h5py'
        module_fpath = '/home/joncrall/code/h5py/build/lib.linux-x86_64-2.7/h5py'
        ut.ls(module_fpath)
        imp.find_module('h5py', '/home/joncrall/code/h5py/')


        # Define two temporary modules that are not in sys.path
        # and have the same name but different values.
        import sys, os, os.path
        def ensuredir(path):
            if not os.path.exists(path):
                os.mkdir(path)
        ensuredir('tmp')
        ensuredir('tmp/tmp1')
        ensuredir('tmp/tmp2')
        ensuredir('tmp/tmp1/testmod')
        ensuredir('tmp/tmp2/testmod')
        with open('tmp/tmp1/testmod/__init__.py', 'w') as file_:
            file_.write('foo = \"spam\"\nfrom . import sibling')
        with open('tmp/tmp1/testmod/sibling.py', 'w') as file_:
            file_.write('bar = \"ham\"')
        with open('tmp/tmp2/testmod/__init__.py', 'w') as file_:
            file_.write('foo = \"eggs\"\nfrom . import sibling')
        with open('tmp/tmp1/testmod/sibling.py', 'w') as file_:
            file_.write('bar = \"jam\"')

        # Neither module should be importable through the normal mechanism
        try:
            import testmod
            assert False, 'should fail'
        except ImportError as ex:
            pass

        # Try temporarilly adding the directory of a module to the path
        sys.path.insert(0, 'tmp/tmp1')
        testmod1 = __import__('testmod', globals(), locals(), 0)
        sys.path.remove('tmp/tmp1')
        print(testmod1.foo)
        print(testmod1.sibling.bar)

        sys.path.insert(0, 'tmp/tmp2')
        testmod2 = __import__('testmod', globals(), locals(), 0)
        sys.path.remove('tmp/tmp2')
        print(testmod2.foo)
        print(testmod2.sibling.bar)

        assert testmod1.foo == "spam"
        assert testmod1.sibling.bar == "ham"

        # Fails, returns spam
        assert testmod2.foo == "eggs"
        assert testmod2.sibling.bar == "jam"

        sys.path.append('/home/username/code/h5py')
        import h5py
        sys.path.append('/home/username/code/h5py/build/lib.linux-x86_64-2.7/')
        import h5py
    """
    from os.path import basename, splitext, isdir, join, exists, dirname, split
    import platform
    if isdir(module_fpath):
        module_fpath = join(module_fpath, '__init__.py')
    print('module_fpath = %r' % (module_fpath,))
    assert exists(module_fpath), 'module_fpath=%r does not exist' % (module_fpath,)
    python_version = platform.python_version()
    modname = splitext(basename(module_fpath))[0]
    if modname == '__init__':
        modname = split(dirname(module_fpath))[1]
    if util_inject.PRINT_INJECT_ORDER:
        if modname not in sys.argv:
            util_inject.noinject(modname, N=2, via='ut.import_module_from_fpath')
    if python_version.startswith('2.7'):
        import imp
        module = imp.load_source(modname, module_fpath)
    elif python_version.startswith('3'):
        import importlib.machinery
        loader = importlib.machinery.SourceFileLoader(modname, module_fpath)
        module = loader.load_module()
    else:
        raise AssertionError('invalid python version=%r' % (python_version,))
    return module
Exemple #6
0
import threading
from six.moves import map, range, zip  # NOQA
from utool._internal.meta_util_six import get_funcname
from utool import util_progress
from utool import util_time
from utool import util_arg
from utool import util_dbg
from utool import util_inject
from utool import util_cplat
if six.PY2:
    import thread as _thread
    import Queue as queue
elif six.PY3:
    import _thread
    import queue
util_inject.noinject('[parallel]')

QUIET = util_arg.QUIET
SILENT = util_arg.SILENT
VERBOSE_PARALLEL, VERYVERBOSE_PARALLEL = util_arg.get_module_verbosity_flags(
    'par', 'parallel')
#VERBOSE_PARALLEL = util_arg.VERBOSE or util_arg.get_argflag(('--verbose-par', '--verbpar', '--verbose-parallel', '--verbparallel'))
#VERYVERBOSE_PARALLEL = util_arg.VERYVERBOSE or util_arg.get_argflag(('--veryverbose-par', '--veryverbpar', '--veryverbose-parallel', '--veryverbparallel'))
STRICT = util_arg.STRICT

if SILENT:

    def print(msg):
        pass

Exemple #7
0
    DynStruct probably needs to go away.
"""
from __future__ import absolute_import, division, print_function, unicode_literals
import six
from six.moves import cPickle as pickle
try:
    import numpy as np
except ImportError as ex:
    pass
from utool import DynamicStruct
from utool import util_dbg
from utool import util_arg
from utool import util_type
from utool import util_inject
# print, rrr, profile = util_inject.inject(__name__, '[pref]')
util_inject.noinject(__name__, '[pref]')

# ---
# GLOBALS
# ---
PrefNode = DynamicStruct.DynStruct

VERBOSE_PREF = util_arg.get_argflag('--verbpref')


# ---
# Classes
# ---
class PrefInternal(DynamicStruct.DynStruct):
    def __init__(_intern, name, doc, default, hidden, fpath, depeq, choices):
        super(PrefInternal, _intern).__init__(child_exclude_list=[])