def _ResolveRelativeImport(name, package):
  """Resolves a relative import into an absolute path.

  This is mostly an adapted version of the logic found in the backported
  version of import_module in Python 2.7.
  https://github.com/python/cpython/blob/2.7/Lib/importlib/__init__.py

  Args:
    name: relative name imported, such as '.a' or '..b.c'
    package: absolute package path, such as 'a.b.c.d.e'

  Returns:
    The absolute path of the name to be imported, or None if it is invalid.
    Examples:
      _ResolveRelativeImport('.c', 'a.b') -> 'a.b.c'
      _ResolveRelativeImport('..c', 'a.b') -> 'a.c'
      _ResolveRelativeImport('...c', 'a.c') -> None
  """
  level = sum(1 for c in itertools.takewhile(lambda c: c == '.', name))
  if level == 1:
    return package + name
  else:
    parts = package.split('.')[:-(level - 1)]
    if not parts:
      return None
    parts.append(name[level:])
    return '.'.join(parts)
Resolves a relative import into an absolute path.

  This is mostly an adapted version of the logic found in the backported
  version of import_module in Python 2.7.
  https://github.com/python/cpython/blob/2.7/Lib/importlib/__init__.py

  Args:
    name: relative name imported, such as '.a' or '..b.c'
    package: absolute package path, such as 'a.b.c.d.e'

  Returns:
    The absolute path of the name to be imported, or None if it is invalid.
    Examples:
      _ResolveRelativeImport('.c', 'a.b') -> 'a.b.c'
      _ResolveRelativeImport('..c', 'a.b') -> 'a.c'
      _ResolveRelativeImport('...c', 'a.c') -> None