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


Groups > comp.lang.python > #21784

Re: avoid import short-circuiting

References <4F636F0D.3060006@gmail.com> <jjvsnj$v9d$1@dough.gmane.org> <4F63B8D4.8010103@gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2012-03-16 16:18 -0600
Subject Re: avoid import short-circuiting
Newsgroups comp.lang.python
Message-ID <mailman.741.1331936319.3037.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, Mar 16, 2012 at 4:04 PM, Andrea Crotti
<andrea.crotti.0@gmail.com> wrote:
>> 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?

You didn't actually monkey-patch it.  You just created a local called
__import__ that stores a wrapped version of the function.  You need to
actually replace it in the __builtin__ module:

import __builtin__
__builtin__.__import__ = my_import

Cheers,
Ian

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


Thread

Re: avoid import short-circuiting Ian Kelly <ian.g.kelly@gmail.com> - 2012-03-16 16:18 -0600

csiph-web