MCPcopy Index your code
hub / github.com/RustPython/RustPython / __import__

Function __import__

Lib/importlib/_bootstrap.py:1465–1499  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

1463
1464
1465def __import__(name, globals=None, locals=None, fromlist=(), level=0):
1466 """Import a module.
1467
1468 The 'globals' argument is used to infer where the import is occurring from
1469 to handle relative imports. The 'locals' argument is ignored. The
1470 'fromlist' argument specifies what should exist as attributes on the module
1471 being imported (e.g. ``from module import <fromlist>``). The 'level'
1472 argument represents the package location to import from in a relative
1473 import (e.g. ``from ..pkg import mod`` would have a 'level' of 2).
1474
1475 """
1476 if level == 0:
1477 module = _gcd_import(name)
1478 else:
1479 globals_ = globals if globals is not None else {}
1480 package = _calc___package__(globals_)
1481 module = _gcd_import(name, package, level)
1482 if not fromlist:
1483 # Return up to the first dot in 'name'. This is complicated by the fact
1484 # that 'name' may be relative.
1485 if level == 0:
1486 return _gcd_import(name.partition('.')[0])
1487 elif not name:
1488 return module
1489 else:
1490 # Figure out where to slice the module's name up to the first dot
1491 # in 'name'.
1492 cut_off = len(name) - len(name.partition('.')[0])
1493 # Slice end needs to be positive to alleviate need to special-case
1494 # when ``'.' not in name``.
1495 return sys.modules[module.__name__[:len(module.__name__)-cut_off]]
1496 elif hasattr(module, '__path__'):
1497 return _handle_fromlist(module, fromlist, _gcd_import)
1498 else:
1499 return module
1500
1501
1502def _builtin_from_name(name):

Callers 15

find_specFunction · 0.70
walk_packagesFunction · 0.50
runMethod · 0.50
_normalize_moduleFunction · 0.50
_testFunction · 0.50
_get_module_detailsFunction · 0.50
whichmoduleFunction · 0.50
find_classMethod · 0.50
_getcategoryFunction · 0.50
read_moduleMethod · 0.50
search_functionFunction · 0.50

Calls 6

lenFunction · 0.85
hasattrFunction · 0.85
_gcd_importFunction · 0.70
_calc___package__Function · 0.70
_handle_fromlistFunction · 0.70
partitionMethod · 0.45

Tested by 15

_normalize_moduleFunction · 0.40
_testFunction · 0.40
getmoduleFunction · 0.40
check_encodingMethod · 0.40
_check_moduleMethod · 0.40
_check_packageMethod · 0.40
checkModuleMethod · 0.40
test_importMethod · 0.40