Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #21782

Re: avoid import short-circuiting

Date 2012-03-16 22:04 +0000
From Andrea Crotti <andrea.crotti.0@gmail.com>
Subject Re: avoid import short-circuiting
References <4F636F0D.3060006@gmail.com> <jjvsnj$v9d$1@dough.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.739.1331935463.3037.python-list@python.org> (permalink)

Show all headers | View raw


On 03/16/2012 05:19 PM, Robert Kern wrote:
> 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.
>

Seems like a good idea :)

My first attempt failes though


def full(module):
     from __builtin__ import __import__
     ls = []
     orig = __import__

     def my_import(name):
         ls.append(name)
         orig(name)

     __import__ = my_import
     __import__(module)
     __import__ = orig
     return ls


it imports only the first element and doesn't import the dependencies..
Any hints?

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: avoid import short-circuiting Andrea Crotti <andrea.crotti.0@gmail.com> - 2012-03-16 22:04 +0000

csiph-web