Navigation

Deprecated since version 2.6: The imputil module has been removed in Python 3.

This module provides a very handy and useful mechanism for custom
import hooks. Compared to the older ihooks module,
imputil takes a dramatically simpler and more straight-forward
approach to custom import functions.

parent specifies a parent module to define a context for importing.
It may be None, indicating no particular context for the search.

modname specifies a single module (not dotted) within the parent.

fqname specifies the fully-qualified module name. This is a
(potentially) dotted name from the “root” of the module namespace
down to the modname.

If there is no parent, then modname==fqname.

This method should return None, or a 3-tuple.

If the module was not found, then None should be returned.

The first item of the 2- or 3-tuple should be the integer 0 or 1,
specifying whether the module that was found is a package or not.

The second item is the code object for the module (it will be
executed within the new module’s namespace). This item can also
be a fully-loaded module object (e.g. loaded from a shared lib).

The third item is a dictionary of name/value pairs that will be
inserted into new module before the code object is executed. This
is provided in case the module’s code expects certain values (such
as where the module was found). When the second item is a module
object, then these names/values will be inserted after the module
has been loaded/initialized.

This code is intended to be read, not executed. However, it does work
– all you need to do to enable it is “import knee”.

(The name is a pun on the clunkier predecessor of this module, “ni”.)

importsys,imp,__builtin__# Replacement for __import__()defimport_hook(name,globals=None,locals=None,fromlist=None):parent=determine_parent(globals)q,tail=find_head_package(parent,name)m=load_tail(q,tail)ifnotfromlist:returnqifhasattr(m,"__path__"):ensure_fromlist(m,fromlist)returnmdefdetermine_parent(globals):ifnotglobalsornotglobals.has_key("__name__"):returnNonepname=globals['__name__']ifglobals.has_key("__path__"):parent=sys.modules[pname]assertglobalsisparent.__dict__returnparentif'.'inpname:i=pname.rfind('.')pname=pname[:i]parent=sys.modules[pname]assertparent.__name__==pnamereturnparentreturnNonedeffind_head_package(parent,name):if'.'inname:i=name.find('.')head=name[:i]tail=name[i+1:]else:head=nametail=""ifparent:qname="%s.%s"%(parent.__name__,head)else:qname=headq=import_module(head,qname,parent)ifq:returnq,tailifparent:qname=headparent=Noneq=import_module(head,qname,parent)ifq:returnq,tailraiseImportError("No module named "+qname)defload_tail(q,tail):m=qwhiletail:i=tail.find('.')ifi<0:i=len(tail)head,tail=tail[:i],tail[i+1:]mname="%s.%s"%(m.__name__,head)m=import_module(head,mname,m)ifnotm:raiseImportError("No module named "+mname)returnmdefensure_fromlist(m,fromlist,recursive=0):forsubinfromlist:ifsub=="*":ifnotrecursive:try:all=m.__all__exceptAttributeError:passelse:ensure_fromlist(m,all,1)continueifsub!="*"andnothasattr(m,sub):subname="%s.%s"%(m.__name__,sub)submod=import_module(sub,subname,m)ifnotsubmod:raiseImportError("No module named "+subname)defimport_module(partname,fqname,parent):try:returnsys.modules[fqname]exceptKeyError:passtry:fp,pathname,stuff=imp.find_module(partname,parentandparent.__path__)exceptImportError:returnNonetry:m=imp.load_module(fqname,fp,pathname,stuff)finally:iffp:fp.close()ifparent:setattr(parent,partname,m)returnm# Replacement for reload()defreload_hook(module):name=module.__name__if'.'notinname:returnimport_module(name,name,None)i=name.rfind('.')pname=name[:i]parent=sys.modules[pname]returnimport_module(name[i+1:],name,parent)# Save the original hooksoriginal_import=__builtin__.__import__original_reload=__builtin__.reload# Now install our hooks__builtin__.__import__=import_hook__builtin__.reload=reload_hook

Also see the importers module (which can be found
in Demo/imputil/ in the Python source distribution) for additional
examples.