MCPcopy Index your code
hub / github.com/python/cpython / __import__

Function __import__

Lib/importlib/_bootstrap.py:1384–1418  ·  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

1382
1383
1384def __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
1421def _builtin_from_name(name):

Callers 15

walk_packagesFunction · 0.85
runMethod · 0.85
read_docstringsFunction · 0.85
_normalize_moduleFunction · 0.85
_testFunction · 0.85
_get_module_detailsFunction · 0.85
whichmoduleFunction · 0.85
find_classMethod · 0.85
_getcategoryFunction · 0.85
read_moduleMethod · 0.85
search_functionFunction · 0.85

Calls 4

_gcd_importFunction · 0.85
_calc___package__Function · 0.85
_handle_fromlistFunction · 0.85
partitionMethod · 0.45

Tested by 15

_normalize_moduleFunction · 0.68
_testFunction · 0.68
test_builtin__import__Function · 0.68
getmoduleFunction · 0.68
check_encodingMethod · 0.68
_check_moduleMethod · 0.68
_check_packageMethod · 0.68
checkModuleMethod · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…