[PEAK] Exceptions in lazy imported module not reported

Phillip J. Eby pje at telecommunity.com
Sat Dec 23 23:54:59 EST 2006


At 11:45 PM 12/23/2006 -0500, Phillip J. Eby wrote:
>class LazyImporter:
>     def __init__(self, names):
>         self.names = dict.fromkeys(names)
>     def find_module(self, fullname, path=None):
>         if fullname in self.names:
>             return self
>     def load_module(self, fullname):
>         self.names.pop(fullname)
>         return lazyModule(fullname)
>
>sys.meta_path.append(
>     LazyImporter(
>         ['pkg1.module1', 'pkg1.module2', ....]
>     )
>)

Actually, I just realized that this is overkill.  You can just as easily do:

map(lazyModule, ['pkg1.module1', ...])

And the effect is roughly the same, except that with the LazyImporter, the 
modules won't be around in sys.modules until an import statement references 
them.

Also, you probably want sys.meta_path.insert(0, LazyImporter(...)) instead 
of append, so that the lazy importer takes precedence over other importers.




More information about the PEAK mailing list