Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Custom Importers You can rebind the _ _import__ attribute of module __builtin_ _ to your own cus- tom importer function by wrapping the __import__ function using the technique shown earlier in this chapter. Such rebinding influences all import and from state- ments that execute after the rebinding. A custom importer must implement the same interface as the built-in __import__ , and is often implemented with some help from the functions exposed by built-in module imp . Custom importer functions are an advanced and rarely used technique. Packages A package is a module that contains other modules. Modules in a package may be subpackages, resulting in a hierarchical tree-like structure. A package named P re- sides in a subdirectory, also called P , of some directory in sys.path . The module body of P is in the file P/__init_ _.py. You must have a file named P/__init_ _.py, even if it's empty (representing an empty module body), in order to indicate to Python that directory P is indeed a package. Other .py files in directory P are the modules of package P . Subdirectories of P containing __init_ _.py files are subpackages of P . Nesting can continue to any depth. You can import a module named M in package P as P . M . More dots let you navigate a hierarchical package structure. A package is always loaded before a module in the package is loaded. If you use the syntax import P . M , variable P is bound to the module