[IPython-dev] ip.core.ipapi.get

Darren Dale dsdale24 at gmail.com
Wed Sep 30 13:43:41 EDT 2009


My use case is to check in a package's __init__.py to see if it is
being imported within an ipython session, and if so to load a custom
completer. I don't think get_ipython will work:

import IPython as ip

ip.InteractiveShell.get_ipython()
------------------------------------------------------------
Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
TypeError: unbound method get_ipython() must be called with
InteractiveShell instance as first argument (got nothing instead)

Am I using it incorrectly?


On Wed, Sep 30, 2009 at 1:21 PM, Brian Granger <ellisonbg.net at gmail.com> wrote:
> This function is essentially deprecated.  This is a bug though that
> you are getting an exception.  But, in the mean time please call the
> following function:
>
> In [1]: ip = get_ipython()
>
> This function is always available inside IPython and returns basically the
> same
> thing as get used to.
>
> For the curious, the problem with the old ipapi.get is that it assumed that
> there
> was always only ONE ipython and it returned that one.  The new get_ipython
> function is smart: it doesn't assume there is only 1 ipython, and it always
> returns
> the right one.
>
> Cheers,
>
> Brian
>
> On Wed, Sep 30, 2009 at 9:35 AM, Darren Dale <dsdale24 at gmail.com> wrote:
>>
>> I think ip.core.ipapi.get() is behaving differently in the trunk than
>> it did before the refactor. Here is the new implementation:
>>
>> def get():
>>    """Get the most recently created InteractiveShell instance."""
>>    from IPython.core.iplib import InteractiveShell
>>    insts = InteractiveShell.get_instances()
>>    most_recent = insts[0]
>>    for inst in insts[1:]:
>>        if inst.created > most_recent.created:
>>            most_recent = inst
>>    return most_recent
>>
>> If I call get from the python prompt, instead of Ipython, I used to
>> get None, but now I get an error because insts is an empty list so
>> insts[0] raises an IndexError. Perhaps:
>>
>> def get():
>>    """Get the most recently created InteractiveShell instance."""
>>    from IPython.core.iplib import InteractiveShell
>>    insts = InteractiveShell.get_instances()
>>    if not insts:
>>        return None
>>    most_recent = insts[0]
>>    for inst in insts[1:]:
>>        if inst.created > most_recent.created:
>>            most_recent = inst
>>    return most_recent
>>
>>
>> Darren



More information about the IPython-dev mailing list