• Home
  • Raw
  • Download

Lines Matching +full:parent +full:- +full:module

2 :mod:`imputil` --- Import utilities
5 .. module:: imputil
10 The :mod:`imputil` module has been removed in Python 3.
15 This module provides a very handy and useful mechanism for custom
16 :keyword:`import` hooks. Compared to the older :mod:`ihooks` module,
17 :mod:`imputil` takes a dramatically simpler and more straight-forward
44 Import a top-level module.
46 .. method:: Importer.get_code(parent, modname, fqname)
48 Find and retrieve the code for the given module.
50 *parent* specifies a parent module to define a context for importing.
53 *modname* specifies a single module (not dotted) within the parent.
55 *fqname* specifies the fully-qualified module name. This is a
56 (potentially) dotted name from the "root" of the module namespace
59 If there is no parent, then modname==fqname.
61 This method should return ``None``, or a 3-tuple.
63 * If the module was not found, then ``None`` should be returned.
65 * The first item of the 2- or 3-tuple should be the integer 0 or 1,
66 specifying whether the module that was found is a package or not.
68 * The second item is the code object for the module (it will be
69 executed within the new module's namespace). This item can also
70 be a fully-loaded module object (e.g. loaded from a shared lib).
73 inserted into new module before the code object is executed. This
74 is provided in case the module's code expects certain values (such
75 as where the module was found). When the second item is a module
76 object, then these names/values will be inserted *after* the module
82 Emulate the import mechanism for built-in and frozen modules. This is a
83 sub-class of the :class:`Importer` class.
85 .. method:: BuiltinImporter.get_code(parent, modname, fqname)
101 .. _examples-imputil:
104 --------
106 This is a re-implementation of hierarchical module import.
109 -- all you need to do to enable it is "import knee".
111 (The name is a pun on the clunkier predecessor of this module, "ni".)
119 parent = determine_parent(globals)
120 q, tail = find_head_package(parent, name)
133 parent = sys.modules[pname]
134 assert globals is parent.__dict__
135 return parent
139 parent = sys.modules[pname]
140 assert parent.__name__ == pname
141 return parent
144 def find_head_package(parent, name):
152 if parent:
153 qname = "%s.%s" % (parent.__name__, head)
156 q = import_module(head, qname, parent)
158 if parent:
160 parent = None
161 q = import_module(head, qname, parent)
163 raise ImportError("No module named " + qname)
174 raise ImportError("No module named " + mname)
192 raise ImportError("No module named " + subname)
194 def import_module(partname, fqname, parent):
201 parent and parent.__path__)
208 if parent:
209 setattr(parent, partname, m)
214 def reload_hook(module):
215 name = module.__name__
220 parent = sys.modules[pname]
221 return import_module(name[i+1:], name, parent)
233 module: knee
235 Also see the :mod:`importers` module (which can be found