| """Backport of importlib.import_module from 3.x.""" |
| # While not critical (and in no way guaranteed!), it would be nice to keep this |
| # code compatible with Python 2.3. |
| def _resolve_name(name, package, level): |
| """Return the absolute name of the module to be imported.""" |
| if not hasattr(package, 'rindex'): |
| raise ValueError("'package' not set to a string") |
| for x in xrange(level, 1, -1): |
| dot = package.rindex('.', 0, dot) |
| raise ValueError("attempted relative import beyond top-level " |
| return "%s.%s" % (package[:dot], name) |
| def import_module(name, package=None): |
| The 'package' argument is required when performing a relative import. It |
| specifies the package to use as the anchor point from which to resolve the |
| relative import to an absolute import. |
| raise TypeError("relative imports require the 'package' argument") |
| name = _resolve_name(name[level:], package, level) |