Velocity Reviews

Velocity Reviews (
-   Python (
-   -   Simple example of how to use importlib to create a loader (

Paul Moore 03-26-2013 04:23 PM

Simple example of how to use importlib to create a loader
I'm trying to write my own loader using importlib. And frankly, I'm getting nowhere. I'm struggling to understand precisely which methods of the various ABCs I need to implement, and in some cases what they should do.

Could anyone point me to a simple example (say, something that implements zip imports using the zipfile module - that was what I initially tried to get working as a proof of concept).

Where I've got to is the following stub implementation to try to log the sequence of calls:

from import PathEntryFinder, SourceLoader

class MyFinder(PathEntryFinder):
def __init__(self, pathentry):
"""Create a finder for the given `pathentry`"""
if pathentry == 'foo':
raise ImportError
def find_loader(self, fullname):
"""Return a loader for the module `fullname`"""
return MyLoader(), ['foo']

class MyLoader(SourceLoader):
def module_repr(self):
return True
def is_package(self, fullname):
return True
def get_data(self, path):
"""Return an open binary file object for `path`"""
return b"print('hello from foo!')"
def get_filename(self, fullname):
"""Return the filename for module `fullname`"""
return '/this/is/foo'

if __name__ == '__main__':
import sys
sys.path_hooks.append(lambda p: MyFinder(p))
sys.path.insert(0, 'foo')
import a
import a.b
from a.b.c import x

It prints out

>py .\

hello from foo!
Traceback (most recent call last):
File ".\", line 46, in <module>
import a.b
ImportError: No module named 'a.b'

which is a good start, but I don't know why it isn't willing to check for 'a.b' (after all, I tell it that 'a' is a package).

I'm quite probably doing something trivially wrong here - after all, I'm not even *trying* to implement a sane view of a package structure here. But I'd have expected at least a find_loader('a.b') call...

Thanks for any help,

All times are GMT. The time now is 10:23 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.