Import a module. 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.
(name, package=None)
| 106 | |
| 107 | |
| 108 | def import_module(name, package=None): |
| 109 | """Import a module. |
| 110 | |
| 111 | The 'package' argument is required when performing a relative import. It |
| 112 | specifies the package to use as the anchor point from which to resolve the |
| 113 | relative import to an absolute import. |
| 114 | |
| 115 | """ |
| 116 | level = 0 |
| 117 | if name.startswith('.'): |
| 118 | if not package: |
| 119 | msg = ("the 'package' argument is required to perform a relative " |
| 120 | "import for {!r}") |
| 121 | raise TypeError(msg.format(name)) |
| 122 | for character in name: |
| 123 | if character != '.': |
| 124 | break |
| 125 | level += 1 |
| 126 | return _bootstrap._gcd_import(name[level:], package, level) |
| 127 | |
| 128 | |
| 129 | _RELOADING = {} |