def import_module_from_file(modpath, pkg=None, log=lgr.debug):
    'Import provided module given a path\n\n    TODO:\n    - RF/make use of it in pipeline.py which has similar logic\n    - join with import_modules above?\n\n    Parameters\n    ----------\n    pkg: module, optional\n       If provided, and modpath is under pkg.__path__, relative import will be\n       used\n    '
    assert modpath.endswith('.py')
    log(('Importing %s' % modpath))
    modname = basename(modpath)[:(- 3)]
    relmodpath = None
    if pkg:
        for pkgpath in pkg.__path__:
            if path_is_subpath(modpath, pkgpath):
                relmodpath = ('.' + relpath(modpath[:(- 3)], pkgpath).replace(sep, '.'))
                break
    try:
        if relmodpath:
            from importlib import import_module
            mod = import_module(relmodpath, pkg.__name__)
        else:
            dirname_ = dirname(modpath)
            try:
                sys.path.insert(0, dirname_)
                mod = __import__(modname, level=0)
            finally:
                if (dirname_ in sys.path):
                    sys.path.pop(sys.path.index(dirname_))
                else:
                    log(('Expected path %s to be within sys.path, but it was gone!' % dirname_))
    except Exception as e:
        raise RuntimeError(('Failed to import module from %s' % modpath)) from e
    return mod
Import provided module given a path

TODO:
- RF/make use of it in pipeline.py which has similar logic
- join with import_modules above?

Parameters
----------
pkg: module, optional
   If provided, and modpath is under pkg.__path__, relative import will be
   used
def import_module_from_file(modpath, pkg=None, log=lgr.debug):
    'Import provided module given a path\n\n    TODO:\n    - RF/make use of it in pipeline.py which has similar logic\n    - join with import_modules above?\n\n    Parameters\n    ----------\n    pkg: module, optional\n       If provided, and modpath is under pkg.__path__, relative import will be\n       used\n    '
    assert modpath.endswith('.py')
    log(('Importing %s' % modpath))
    modname = basename(modpath)[:(- 3)]
    relmodpath = None
    if pkg:
        for pkgpath in pkg.__path__:
            if path_is_subpath(modpath, pkgpath):
                relmodpath = ('.' + relpath(modpath[:(- 3)], pkgpath).replace(sep, '.'))
                break
    try:
        if relmodpath:
            from importlib import import_module
            mod = import_module(relmodpath, pkg.__name__)
        else:
            dirname_ = dirname(modpath)
            try:
                sys.path.insert(0, dirname_)
                mod = __import__(modname, level=0)
            finally:
                if (dirname_ in sys.path):
                    sys.path.pop(sys.path.index(dirname_))
                else:
                    log(('Expected path %s to be within sys.path, but it was gone!' % dirname_))
    except Exception as e:
        raise RuntimeError(('Failed to import module from %s' % modpath)) from e
    return mod