MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / __import__

Function __import__

tools/python-3.11.9-amd64/Lib/importlib/_bootstrap.py:1271–1305  ·  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 <f

(name, globals=None, locals=None, fromlist=(), level=0)

Source from the content-addressed store, hash-verified

1269
1270
1271def __import__(name, globals=None, locals=None, fromlist=(), level=0):
1272 """Import a module.
1273
1274 The 'globals' argument is used to infer where the import is occurring from
1275 to handle relative imports. The 'locals' argument is ignored. The
1276 'fromlist' argument specifies what should exist as attributes on the module
1277 being imported (e.g. ``from module import <fromlist>``). The 'level'
1278 argument represents the package location to import from in a relative
1279 import (e.g. ``from ..pkg import mod`` would have a 'level' of 2).
1280
1281 """
1282 if level == 0:
1283 module = _gcd_import(name)
1284 else:
1285 globals_ = globals if globals is not None else {}
1286 package = _calc___package__(globals_)
1287 module = _gcd_import(name, package, level)
1288 if not fromlist:
1289 # Return up to the first dot in 'name'. This is complicated by the fact
1290 # that 'name' may be relative.
1291 if level == 0:
1292 return _gcd_import(name.partition('.')[0])
1293 elif not name:
1294 return module
1295 else:
1296 # Figure out where to slice the module's name up to the first dot
1297 # in 'name'.
1298 cut_off = len(name) - len(name.partition('.')[0])
1299 # Slice end needs to be positive to alleviate need to special-case
1300 # when ``'.' not in name``.
1301 return sys.modules[module.__name__[:len(module.__name__)-cut_off]]
1302 elif hasattr(module, '__path__'):
1303 return _handle_fromlist(module, fromlist, _gcd_import)
1304 else:
1305 return module
1306
1307
1308def _builtin_from_name(name):

Callers 15

walk_packagesFunction · 0.85
safeimportFunction · 0.85
runMethod · 0.85
read_docstringsFunction · 0.85
_normalize_moduleFunction · 0.85
_testFunction · 0.85
smtpd.pyFile · 0.85
_getcategoryFunction · 0.85
_get_module_detailsFunction · 0.85
_init_posixFunction · 0.85
save_globalMethod · 0.85
find_classMethod · 0.85

Calls 4

_gcd_importFunction · 0.85
_calc___package__Function · 0.85
_handle_fromlistFunction · 0.85
partitionMethod · 0.80

Tested by 2

_normalize_moduleFunction · 0.68
_testFunction · 0.68