2009-01-18 01:24:28 +01:00
|
|
|
"""A pure Python implementation of import.
|
|
|
|
|
|
|
|
References on import:
|
|
|
|
|
|
|
|
* Language reference
|
|
|
|
http://docs.python.org/ref/import.html
|
|
|
|
* __import__ function
|
|
|
|
http://docs.python.org/lib/built-in-funcs.html
|
|
|
|
* Packages
|
|
|
|
http://www.python.org/doc/essays/packages.html
|
|
|
|
* PEP 235: Import on Case-Insensitive Platforms
|
|
|
|
http://www.python.org/dev/peps/pep-0235
|
|
|
|
* PEP 275: Import Modules from Zip Archives
|
|
|
|
http://www.python.org/dev/peps/pep-0273
|
|
|
|
* PEP 302: New Import Hooks
|
|
|
|
http://www.python.org/dev/peps/pep-0302/
|
|
|
|
* PEP 328: Imports: Multi-line and Absolute/Relative
|
|
|
|
http://www.python.org/dev/peps/pep-0328
|
|
|
|
|
|
|
|
"""
|
2009-03-12 23:01:40 +01:00
|
|
|
__all__ = ['__import__', 'import_module']
|
|
|
|
|
2009-01-18 01:24:28 +01:00
|
|
|
from . import _bootstrap
|
|
|
|
|
|
|
|
|
2012-01-26 00:58:03 +01:00
|
|
|
# To simplify imports in test code
|
|
|
|
_w_long = _bootstrap._w_long
|
|
|
|
_r_long = _bootstrap._r_long
|
|
|
|
|
|
|
|
|
2009-01-22 23:44:04 +01:00
|
|
|
# Bootstrap help #####################################################
|
2012-02-09 00:50:22 +01:00
|
|
|
import imp
|
|
|
|
import sys
|
2009-01-22 23:44:04 +01:00
|
|
|
|
2012-02-09 00:50:22 +01:00
|
|
|
_bootstrap._setup(sys, imp)
|
2009-01-18 01:24:28 +01:00
|
|
|
|
2009-01-20 03:21:27 +01:00
|
|
|
|
2009-01-22 23:44:04 +01:00
|
|
|
# Public API #########################################################
|
|
|
|
|
2009-03-15 01:53:05 +01:00
|
|
|
from ._bootstrap import __import__
|
2009-01-20 03:21:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
def import_module(name, package=None):
|
|
|
|
"""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.
|
|
|
|
|
|
|
|
"""
|
2009-02-07 02:15:27 +01:00
|
|
|
level = 0
|
2009-01-20 03:21:27 +01:00
|
|
|
if name.startswith('.'):
|
|
|
|
if not package:
|
|
|
|
raise TypeError("relative imports require the 'package' argument")
|
|
|
|
for character in name:
|
|
|
|
if character != '.':
|
|
|
|
break
|
|
|
|
level += 1
|
2009-02-07 02:15:27 +01:00
|
|
|
return _bootstrap._gcd_import(name[level:], package, level)
|