1 # Taken from Python 2.7 with permission from/by the original author.
4 def _resolve_name(name
, package
, level
):
5 """Return the absolute name of the module to be imported."""
6 if not hasattr(package
, 'rindex'):
7 raise ValueError("'package' not set to a string")
9 for x
in xrange(level
, 1, -1):
11 dot
= package
.rindex('.', 0, dot
)
13 raise ValueError("attempted relative import beyond top-level "
15 return "%s.%s" % (package
[:dot
], name
)
18 def import_module(name
, package
=None):
21 The 'package' argument is required when performing a relative import. It
22 specifies the package to use as the anchor point from which to resolve the
23 relative import to an absolute import.
26 if name
.startswith('.'):
28 raise TypeError("relative imports require the 'package' argument")
30 for character
in name
:
34 name
= _resolve_name(name
[level
:], package
, level
)
36 return sys
.modules
[name
]