It would be nice to have a simple and easy way to override module importing, instead of that babylon which CPython has in that regard. (Even if initially "limited" to filesystem importing, most people won't ever find that limiting at all.)
The desired semantics would be:
For "import foo", before "/path1/foo.py", "/path2/foo.py" (where /path1:/path2 come from sys.path) is tried to be imported, a user-defined hook function can be called. It should take just one param - full basename of the module to be imported, e.g. "/path1/foo", "/path2/foo" for the cases above. The hook should either return a module object, the result of the import, or None to signal that standard processing should take place. The hook should be aware that there might have been previous hooks, and it's current hook's responsibility to call them.
Example of a hook:
def import_hook(path): if os.path.exists(path + ".my_ext"): mod = imp.new_module("") # populate mod here return mod
A crucial semantic detail is that all possible module types from one sys.path entry should be tried first, before going to the next entry. (CPython import extensibility instead seems to be built around idea that it's fine to do multiple passes over sys.path, loading only a particular extension module type on each pass, which is not the desired semantics).
1. Does anybody know such thing to already exist? 2. Would you tweak anything in the API/boilerplate semantics of the above?