Simple example of how to use importlib to create a loader
p.f.moore at gmail.com
Tue Mar 26 17:23:05 CET 2013
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 importlib.abc import PathEntryFinder, SourceLoader
def __init__(self, pathentry):
"""Create a finder for the given `pathentry`"""
if pathentry == 'foo':
def find_loader(self, fullname):
"""Return a loader for the module `fullname`"""
return MyLoader(), ['foo']
def is_package(self, fullname):
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`"""
if __name__ == '__main__':
sys.path_hooks.append(lambda p: MyFinder(p))
from a.b.c import x
It prints out
hello from foo!
Traceback (most recent call last):
File ".\loader.py", line 46, in <module>
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,
More information about the Python-list