title: How to use sys.path_hooks for customized loading of modules?
body: <p>I hope the following question is not too long. But otherwise I cannot explain by problem and what I want:</p>
  
  <p>Learned from <a href="https://stackoverflow.com/questions/41941079/how-to-use-importlib-to-import-modules-from-arbitrary-sources">How to use importlib to import modules from arbitrary sources?</a> (my question of yesterday)
  I have written a specfic loader for a new file type (.xxx).
  (In fact the xxx is an encrypted version of a pyc to protect code from being stolen).</p>
  
  <p>I would like just to <strong>add</strong> an import hook for the new file type "xxx" without affecting the other types (.py, .pyc, .pyd) in any way.</p>
  
  <p>Now, the loader is <code>ModuleLoader</code>, inheriting from <code>mportlib.machinery.SourcelessFileLoader</code>.</p>
  
  <p>Using <code>sys.path_hooks</code> the loader shall be added as a hook:</p>
  
  <pre><code>myFinder = importlib.machinery.FileFinder
  loader_details = (ModuleLoader, ['.xxx'])
  sys.path_hooks.append(myFinder.path_hook(loader_details))
  </code></pre>
  
  <p>Note: This is activated once by calling <code>modloader.activateLoader()</code></p>
  
  <p>Upon loading a module named <code>test</code> (which is a <code>test.xxx</code>) I get:</p>
  
  <pre class="lang-none prettyprint-override"><code>&gt;&gt;&gt; import modloader
  &gt;&gt;&gt; modloader.activateLoader()
  &gt;&gt;&gt; import test
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  ImportError: No module named 'test'
  &gt;&gt;&gt;
  </code></pre>
  
  <p>However, when I delete content of <code>sys.path_hooks</code> before adding the hook:</p>
  
  <pre><code>sys.path_hooks = []
  sys.path.insert(0, '.') # current directory
  sys.path_hooks.append(myFinder.path_hook(loader_details))
  </code></pre>
  
  <p>it works:</p>
  
  <pre><code>&gt;&gt;&gt; modloader.activateLoader()
  &gt;&gt;&gt; import test
  using xxx class
  
  in xxxLoader exec_module
  in xxxLoader get_code: .\test.xxx
  ANALYZING ...
  
  GENERATE CODE OBJECT ...
  
    2           0 LOAD_CONST               0
                3 LOAD_CONST               1 ('foo2')
                6 MAKE_FUNCTION            0
                9 STORE_NAME               0 (foo2)
               12 LOAD_CONST               2 (None)
               15 RETURN_VALUE
  &gt;&gt;&gt;&gt;&gt;&gt; test
  &lt;module 'test' from '.\\test.xxx'&gt;
  </code></pre>
  
  <p>The module is imported correctly after conversion of the files content to a code object.</p>
  
  <p><strong>However I cannot load the same module from a package:</strong> <code>import pack.test</code></p>
  
  <p>Note: <code>__init__.py</code> is of course as an empty file in pack directory.</p>
  
  <pre class="lang-none prettyprint-override"><code>&gt;&gt;&gt; import pack.test
  Traceback (most recent call last):
    File "&lt;frozen importlib._bootstrap&gt;", line 2218, in _find_and_load_unlocked
  AttributeError: 'module' object has no attribute '__path__'
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  ImportError: No module named 'pack.test'; 'pack' is not a package
  &gt;&gt;&gt;
  </code></pre>
  
  <p>Not enough, I cannot load plain *.py modules from that package anymore: I get the same error as above:</p>
  
  <pre class="lang-none prettyprint-override"><code>&gt;&gt;&gt; import pack.testpy
  Traceback (most recent call last):
    File "&lt;frozen importlib._bootstrap&gt;", line 2218, in _find_and_load_unlocked
  AttributeError: 'module' object has no attribute '__path__'
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  ImportError: No module named 'pack.testpy'; 'pack' is not a package
  &gt;&gt;&gt;
  </code></pre>
  
  <p>For my understanding <code>sys.path_hooks</code> is traversed until the last entry is tried. So why is the first variant (without deleting <code>sys.path_hooks</code>) not recognizing the new extension "xxx" and the second variant (deleting <code>sys.path_hooks</code>) do?
  It looks like the machinery is throwing an exception rather than traversing further to the next entry, when an entry of <code>sys.path_hooks</code> is not able to recognize "xxx".</p>
  
  <p>And why is the second version <strong>working</strong> for py, pyc and xxx modules in the current directory, <strong>but not working</strong> in the package <code>pack</code>? I would expect that py and pyc is not even working in the current dir, because <code>sys.path_hooks</code> contains only a hook for "xxx"...</p>