1
2
3
4 import sys
5
7 """Return the absolute name of the module to be imported."""
8 if not hasattr(package, 'rindex'):
9 raise ValueError("'package' not set to a string")
10 dot = len(package)
11 for x in xrange(level, 1, -1):
12 try:
13 dot = package.rindex('.', 0, dot)
14 except ValueError:
15 raise ValueError("attempted relative import beyond top-level "
16 "package")
17 return "%s.%s" % (package[:dot], name)
18
19
21 """Import a module.
22
23 The 'package' argument is required when performing a relative import. It
24 specifies the package to use as the anchor point from which to resolve the
25 relative import to an absolute import.
26
27 """
28 if name.startswith('.'):
29 if not package:
30 raise TypeError("relative imports require the 'package' argument")
31 level = 0
32 for character in name:
33 if character != '.':
34 break
35 level += 1
36 name = _resolve_name(name[level:], package, level)
37 __import__(name)
38 return sys.modules[name]
39