title: Python Import Hooks and __main__
body: <p><a href="http://www.python.org/dev/peps/pep-0302/" rel="nofollow">PEP 302</a> defines a way for user code to hook into the import process and intercept imports. </p>
  
  <p>However, this only works for imports that occur <em>after</em> some code has run to register the import hook.</p>
  
  <p>Almost by definition, no code runs before the <code>__main__</code> module, which is the script that is initially run. I haven't had any luck trying to get my import hooks (which do some useful things) to intercept the code being loaded for the <code>__main__</code> module.</p>
  
  <p>Is there any way forward that I'm missing, or am I just out of luck? I can accept having my import hooks not perform their transformations on the <code>__main__</code> module, but it would be nice if they did.</p>