(There is further discussion over in the proposal I made to fix this.)
Alas, there are several wrinkles to sort out with your code:
- For efficiency, it needs a cache equivalent to
sys.path_importer_cache - You need to take into account case-insensitivity of extensions under Windows
- As you note in your comments, it doesn’t work for top-level imports
Over in that other thread, Paul Moore mentioned Quixote as a project which implements its own file extensions; it turns out it achieved that relatively heinously by overriding an undocumented function that began with an _.
In general, none of these solutions look especially simple or robust. Whereas I had a go at implementing my proposed enhancement to importlib and the volume of code changes needed was surprisingly small: about a third the size of your solution.