I’m the one who posted the original question on that StackOverflow thread which eventually led me to implement my ideas library which I have mentioned before and which works with Python 3.15 (and can handle source code transformations). It also never calls sys.path_importer_cache.clear().
Inserting a finder as the first item in sys.path_hooks means that it will be the first finder Python will use to attempt to find a script to be imported; if it works, having other finders available is irrelevant as they will not be used.
However, instead of inserting a finder in sys.path_hooks like you do, I insert it in sys.meta_path. Perhaps doing the same might solve your problem.
It isn’t, the problem is in sys.path_hooks.insert(0, FileFinder.path_hook( (PreProcessLoader, \[".py"\]) )). That statement only adds support for .py files.
PEP 302 says this about path hooks:
sys.path_hooks is a list of callables, which will be checked in sequence to determine if they can handle a given path item. The callable is called with one argument, the path item. The callable must raise ImportErrorif it is unable to handle the path item, and return an importer object if it can handle the path item.
The path hook for FileFinder handles every existing directory on sys.path. Because of this your hook replaces the default hook that loads extension modules (and modules that only have a bytecode file)
So the question is then how do I stop my custom FileFinder path hook from intercepting import calls for extension, and other, modules? I would have thought the [".py"] should do that but obviously not.
@jonathandung Only did that to demonstrate for testing; it proved that raising the error doesn’t then pass the path item search onto the next finder as PEP 302 suggests it should.