Import a module. The 'globals' argument is used to infer where the import is occurring from to handle relative imports. The 'locals' argument is ignored. The 'fromlist' argument specifies what should exist as attributes on the module being imported (e.g. ``from module import <fromli
(name, globals=None, locals=None, fromlist=(), level=0)
| 1382 | |
| 1383 | |
| 1384 | def __import__(name, globals=None, locals=None, fromlist=(), level=0): |
| 1385 | """Import a module. |
| 1386 | |
| 1387 | The 'globals' argument is used to infer where the import is occurring from |
| 1388 | to handle relative imports. The 'locals' argument is ignored. The |
| 1389 | 'fromlist' argument specifies what should exist as attributes on the module |
| 1390 | being imported (e.g. ``from module import <fromlist>``). The 'level' |
| 1391 | argument represents the package location to import from in a relative |
| 1392 | import (e.g. ``from ..pkg import mod`` would have a 'level' of 2). |
| 1393 | |
| 1394 | """ |
| 1395 | if level == 0: |
| 1396 | module = _gcd_import(name) |
| 1397 | else: |
| 1398 | globals_ = globals if globals is not None else {} |
| 1399 | package = _calc___package__(globals_) |
| 1400 | module = _gcd_import(name, package, level) |
| 1401 | if not fromlist: |
| 1402 | # Return up to the first dot in 'name'. This is complicated by the fact |
| 1403 | # that 'name' may be relative. |
| 1404 | if level == 0: |
| 1405 | return _gcd_import(name.partition('.')[0]) |
| 1406 | elif not name: |
| 1407 | return module |
| 1408 | else: |
| 1409 | # Figure out where to slice the module's name up to the first dot |
| 1410 | # in 'name'. |
| 1411 | cut_off = len(name) - len(name.partition('.')[0]) |
| 1412 | # Slice end needs to be positive to alleviate need to special-case |
| 1413 | # when ``'.' not in name``. |
| 1414 | return sys.modules[module.__name__[:len(module.__name__)-cut_off]] |
| 1415 | elif hasattr(module, '__path__'): |
| 1416 | return _handle_fromlist(module, fromlist, _gcd_import) |
| 1417 | else: |
| 1418 | return module |
| 1419 | |
| 1420 | |
| 1421 | def _builtin_from_name(name): |
searching dependent graphs…