Help on import hooks

Gonçalo Rodrigues op73418 at
Mon Nov 17 00:59:04 CET 2003


I need to extend the importing mechanism. More precisely, I'm working
on a plugin architecture. Plugins are just normal python files but
with extension ".plg". I want them to be loaded via the import

import <some plugin name>

should trigger the following:

- In all respects the first part is *exactly* like a usual module:
Consult sys.modules, search the Python path for the module, read the
file, instantiate the module, put it in sys.modules, execute the code
in the module.

(In the future I may want to cache these plugins in a different place,
instead of sys.modules, but for now this is good enough)

- After the above, do some extra processing.

New import hooks to the rescue. Now, I've been reading PEP 302 and
from what I've gathered the extra processing stuff is to be handled by
the loader object - and that I can understand . But I'm at a loss on
what to do with the importer object

- Since these plugin modules are special-cased by their file extension
do I have to register an importer in sys.meta_path? Do I even need an

- And how do implement the find_module in the importer? What is the
significance of the second path arg to find_module? Is it the same as
in imp.find_module, that is, a list of directories to search? 

Skeleton code to tackle this would be a great help.

TIA, with my best regards,
G. Rodrigues

More information about the Python-list mailing list