Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'imports': 0.07; 'infinite': 0.07; 'mess': 0.07; 'called.': 0.09; 'myself,': 0.09; 'url:github': 0.09; 'def': 0.13; 'anymore.': 0.16; 'kern': 0.16; 'subject:import': 0.16; 'to:addr:robert.kern': 0.16; 'to:name:robert kern': 0.16; 'cc:addr:python-list': 0.16; 'examples': 0.16; 'wrote:': 0.18; 'this?': 0.19; 'seems': 0.20; 'cc:no real name:2**0': 0.21; "doesn't": 0.22; 'header:In-Reply- To:1': 0.22; 'guess': 0.26; 'cc:2**0': 0.26; 'module': 0.26; 'skip:_ 20': 0.26; 'import': 0.27; 'tried': 0.27; 'depends': 0.28; 'problem': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'imported': 0.30; 'message-id:@gmail.com': 0.31; 'quite': 0.31; 'instead.': 0.32; 'idea': 0.32; 'there': 0.33; 'received:209.85.212': 0.33; 'header:User-Agent:1': 0.33; 'too': 0.33; 'loop': 0.34; 'element': 0.37; 'but': 0.37; 'received:google.com': 0.37; 'using': 0.37; 'skip:_ 10': 0.38; 'received:209.85': 0.38; 'received:192': 0.38; 'could': 0.38; 'should': 0.38; 'received:192.168.1': 0.39; 'received:209': 0.39; 'might': 0.40; 'easy': 0.60; 'achieve': 0.61; 'andrea': 0.84; 'hints?': 0.84; 'hook': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=JQeJh96feF/QoookmMzTsi27uEiM+7hWpUC8dkZvDH0=; b=cDxgSX2mQy/ko1e10Vvd9KaVIkVhn3DkKglyextvNP4HvXF7uh7AcU9oQznNFQ+PkQ okfQsgMWQk0tuXMc3Jm2AV/w1TXzYDmWIL5GXxEz67L1dFSq72uChhhYf3XgS+j5QXFB zXk9Dyo1ktN3RTgM4jrW0JWUWGmrBVHCCW8xwZ9KQIg/k0Z1W4z1+glQAPDoDVQqbHYm FyOvh9oBzBGOVSTvHds/UWDxBxaqMKgzuT09g6+g6zbND0oZpNp0QqC9d/64vBiknN5Y EQdPKNnouTe0GP5l4jXyF/EomykyEeSSlzUikv8NmWjjQcPiH+RdPkgmltwfh82M3UIk yJUw== Date: Fri, 16 Mar 2012 22:04:04 +0000 From: Andrea Crotti User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:11.0) Gecko/20120314 Thunderbird/11.0 MIME-Version: 1.0 To: Robert Kern Subject: Re: avoid import short-circuiting References: <4F636F0D.3060006@gmail.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1331935463 news.xs4all.nl 6871 [2001:888:2000:d::a6]:50643 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:21782 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?