avoid import short-circuiting

Robert Kern robert.kern at gmail.com
Fri Mar 16 18:19:47 CET 2012

On 3/16/12 4:49 PM, Andrea Crotti wrote:
> I started the following small project:
> https://github.com/AndreaCrotti/import-tree
> because I would like to find out what exactly depends on what at run-time, using
> an import hook.
> It works quite well for small examples but the main problem is that once a
> module is imported
> it's added to sys.modules and then it doesn't go through the import hook anymore.
> I tried to mess around with sys.modules but it might not be a good idea, and it
> leads to easy
> infinite loops.
> Is there a good way to achieve this?
> I guess I could do the loop detection myself, but that should not be too hard..

You want to monkeypatch __builtin__.__import__() instead. It always gets called.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

More information about the Python-list mailing list