Re: Accessing get_ipython() within an embedded shell

Thomas Kluyver <[email protected]> Tue, 10 Apr 2018 21:18:04 +0200
Newsgroups gmane.comp.python.ipython.devel
Message-ID <CAOvn4qh59mJdQwRPj+o6TRTda84GQtMWdyNWohH7RcOE-K9tfA@mail.gmail.com>
--===============0503904319789880199==
Content-Type: multipart/alternative; boundary="f4f5e8075c4086e84d0569836328"

--f4f5e8075c4086e84d0569836328
Content-Type: text/plain; charset="UTF-8"

On this topic, it bears reiterate that 'embedding' IPython has a specific
purpose: to run IPython with the namespace where it was called, like a
debugger with no stepping forwards.

If you want to run IPython programmatically with its own namespace, you
don't need to 'embed' it, and embedding has extra complexities that might
confuse things. You can still access the namespace of a regular IPython
shell object as ip.user_ns .

On 10 April 2018 at 20:41, Jacob Vanderplas <[email protected]>
wrote:

> Wes,
> Thanks for the links! I found the clue I needed in the pytest-ipdb link.
>
> Instead of using:
>
>   from IPython.terminal.embed import InteractiveShellEmbed
>   ipshell = InteractiveShellEmbed()
>
> I used this:
>
>   from IPython import InteractiveShell
>   ipshell = InteractiveShell.instance()
>
> And now calls to IPython.get_ipython() within the shell work properly.
>
> Thanks,
>     Jake
>
>  Jake VanderPlas
>  Senior Data Science Fellow
>  Director of Open Software
>  University of Washington eScience Institute
>
> On Tue, Apr 10, 2018 at 2:22 AM, Wes Turner <[email protected]> wrote:
>
>> ipdb or pytest-ipdb may have a helpful example?
>>
>> https://github.com/gotcha/ipdb/blob/master/ipdb/__main__.py
>>
>> https://github.com/mverteuil/pytest-ipdb/blob/master/pytestipdb/ptipdb.py
>>
>> These all require nose:
>>
>> https://github.com/ipython/ipython/blob/master/IPython/core/
>> tests/test_magic_terminal.py
>>
>> https://github.com/ipython/ipython/blob/master/IPython/core/
>> tests/test_magic_arguments.py
>>
>> https://github.com/ipython/ipython/blob/master/IPython/core/
>> tests/test_interactiveshell.py
>>
>>
>> On Tuesday, April 10, 2018, Jacob Vanderplas <[email protected]>
>> wrote:
>>
>>> I'm trying to create automated tests for some magic functions within a
>>> pytest framework. I've figured out how to do part of what I need using
>>> an embedded interactive shell; briefly, I do something like this:
>>>
>>> from IPython.terminal.embed import InteractiveShellEmbed
>>> ipshell = InteractiveShellEmbed()
>>> out = ipshell.run_cell("%my_magic_command")
>>> assert out.result == expected_result
>>>
>>> The problem I encounter is that my magic function optionally uses
>>> IPython.get_ipython().user_ns to access variables in the user
>>> namespace, and IPython.get_ipython() returns None when run within an
>>> embedded shell in this way.
>>>
>>> Is there a way to create a simple embedded shell like this one that will
>>> behave as if it is an actual IPython shell, with IPython.get_ipython()
>>> defined?
>>>
>>> Thanks,
>>>
>>>   Jake
>>>
>>
>> _______________________________________________
>> IPython-dev mailing list
>> [email protected]
>> https://mail.python.org/mailman/listinfo/ipython-dev
>>
>>
>
> _______________________________________________
> IPython-dev mailing list
> [email protected]
> https://mail.python.org/mailman/listinfo/ipython-dev
>
>

--f4f5e8075c4086e84d0569836328
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>On this topic, it bears reiterate that &#39;embedding=
&#39; IPython has a specific purpose: to run IPython with the namespace whe=
re it was called, like a debugger with no stepping forwards.<br><br></div>I=
f you want to run IPython programmatically with its own namespace, you don&=
#39;t need to &#39;embed&#39; it, and embedding has extra complexities that=
 might confuse things. You can still access the namespace of a regular IPyt=
hon shell object as ip.user_ns .<br></div><div class=3D"gmail_extra"><br><d=
iv class=3D"gmail_quote">On 10 April 2018 at 20:41, Jacob Vanderplas <span =
dir=3D"ltr">&lt;<a href=3D"mailto:[email protected]" target=3D"_bla=
nk">[email protected]</a>&gt;</span> wrote:<br><blockquote class=3D=
"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding=
-left:1ex"><div dir=3D"ltr">Wes,<div>Thanks for the links! I found the clue=
 I needed in the pytest-ipdb link.</div><div><br></div><div>Instead of usin=
g:</div><div><br></div><div><span class=3D""><div style=3D"color:rgb(34,34,=
34);font-family:arial,sans-serif;font-size:12.8px;font-style:normal;font-va=
riant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spac=
ing:normal;text-align:start;text-indent:0px;text-transform:none;white-space=
:normal;word-spacing:0px;text-decoration-style:initial;text-decoration-colo=
r:initial"><font face=3D"monospace, monospace">=C2=A0 from<span>=C2=A0</spa=
n><span class=3D"m_9088155197930985685gmail-m_-3560420487135173310gmail-il"=
><span class=3D"m_9088155197930985685gmail-il">IPython</span></span>.termin=
al.embed import InteractiveShellEmbed</font></div><div style=3D"color:rgb(3=
4,34,34);font-family:arial,sans-serif;font-size:12.8px;font-style:normal;fo=
nt-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter=
-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-=
space:normal;word-spacing:0px;text-decoration-style:initial;text-decoration=
-color:initial"><font face=3D"monospace, monospace">=C2=A0 ipshell =3D Inte=
ractiveShellEmbed()</font></div><div style=3D"color:rgb(34,34,34);font-fami=
ly:arial,sans-serif;font-size:12.8px;font-style:normal;font-variant-ligatur=
es:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;te=
xt-align:start;text-indent:0px;text-transform:none;white-space:normal;word-=
spacing:0px;text-decoration-style:initial;text-decoration-color:initial"><f=
ont face=3D"monospace, monospace"><br></font></div></span><div style=3D"col=
or:rgb(34,34,34);font-size:12.8px;font-style:normal;font-variant-ligatures:=
normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-=
align:start;text-indent:0px;text-transform:none;white-space:normal;word-spa=
cing:0px;text-decoration-style:initial;text-decoration-color:initial"><font=
 face=3D"arial, helvetica, sans-serif">I used this:</font></div><div style=
=3D"text-align:start;text-indent:0px;text-decoration-style:initial;text-dec=
oration-color:initial"><span style=3D"font-size:12.8px"><div style=3D"font-=
family:arial,helvetica,sans-serif"><br></div><div><font face=3D"monospace, =
monospace">=C2=A0 from IPython import InteractiveShell</font></div><div><fo=
nt face=3D"monospace, monospace">=C2=A0 ipshell =3D InteractiveShell.instan=
ce()</font></div></span></div><div style=3D"color:rgb(34,34,34);font-size:1=
2.8px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:nor=
mal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;=
text-transform:none;white-space:normal;word-spacing:0px;text-decoration-sty=
le:initial;text-decoration-color:initial"><font face=3D"arial, helvetica, s=
ans-serif"><br></font></div><div style=3D"color:rgb(34,34,34);font-size:12.=
8px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:norma=
l;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;te=
xt-transform:none;white-space:normal;word-spacing:0px;text-decoration-style=
:initial;text-decoration-color:initial"><font face=3D"arial, helvetica, san=
s-serif">And now calls to IPython.get_ipython() within the shell work prope=
rly.</font></div><div style=3D"color:rgb(34,34,34);font-size:12.8px;font-st=
yle:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weig=
ht:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transfor=
m:none;white-space:normal;word-spacing:0px;text-decoration-style:initial;te=
xt-decoration-color:initial"><font face=3D"arial, helvetica, sans-serif"><b=
r></font></div><div style=3D"color:rgb(34,34,34);font-size:12.8px;font-styl=
e:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight=
:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:=
none;white-space:normal;word-spacing:0px;text-decoration-style:initial;text=
-decoration-color:initial"><font face=3D"arial, helvetica, sans-serif">Than=
ks,</font></div><div style=3D"color:rgb(34,34,34);font-size:12.8px;font-sty=
le:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weigh=
t:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform=
:none;white-space:normal;word-spacing:0px;text-decoration-style:initial;tex=
t-decoration-color:initial"><font face=3D"arial, helvetica, sans-serif">=C2=
=A0 =C2=A0 Jake</font></div></div></div><div class=3D"gmail_extra"><br clea=
r=3D"all"><div><div class=3D"m_9088155197930985685gmail_signature" data-sma=
rtmail=3D"gmail_signature"><div dir=3D"ltr"><div><div dir=3D"ltr"><div dir=
=3D"ltr">=C2=A0<font size=3D"1">Jake VanderPlas</font></div><div dir=3D"ltr=
"><font size=3D"1">=C2=A0Senior Data Science Fellow</font></div><div dir=3D=
"ltr"><span style=3D"font-size:x-small">=C2=A0Director of Open Software</sp=
an><div><div><font size=3D"1">=C2=A0</font><span style=3D"font-size:x-small=
">University of Washington=C2=A0</span><span style=3D"font-size:x-small">eS=
cience Institute<br></span></div></div></div></div></div></div></div></div>
<br><div class=3D"gmail_quote"><div><div class=3D"h5">On Tue, Apr 10, 2018 =
at 2:22 AM, Wes Turner <span dir=3D"ltr">&lt;<a href=3D"mailto:wes.turner@g=
mail.com" target=3D"_blank">[email protected]</a>&gt;</span> wrote:<br><=
/div></div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;bor=
der-left:1px #ccc solid;padding-left:1ex"><div><div class=3D"h5"><div>ipdb =
or pytest-ipdb may have a helpful example?</div><div><br></div><a href=3D"h=
ttps://github.com/gotcha/ipdb/blob/master/ipdb/__main__.py" target=3D"_blan=
k">https://github.com/gotcha/ipdb<wbr>/blob/master/ipdb/__main__.py</a><div=
><br></div><div><a href=3D"https://github.com/mverteuil/pytest-ipdb/blob/ma=
ster/pytestipdb/ptipdb.py" target=3D"_blank">https://github.com/mverteuil/p=
<wbr>ytest-ipdb/blob/master/pytesti<wbr>pdb/ptipdb.py</a></div><div><br></d=
iv><div>These all require nose:</div><div><br></div><div><a href=3D"https:/=
/github.com/ipython/ipython/blob/master/IPython/core/tests/test_magic_termi=
nal.py" target=3D"_blank">https://github.com/ipython/ipy<wbr>thon/blob/mast=
er/IPython/core/<wbr>tests/test_magic_terminal.py</a></div><div><br></div><=
div><a href=3D"https://github.com/ipython/ipython/blob/master/IPython/core/=
tests/test_magic_arguments.py" target=3D"_blank">https://github.com/ipython=
/ipy<wbr>thon/blob/master/IPython/core/<wbr>tests/test_magic_arguments.py</=
a></div><div><br></div><div><a href=3D"https://github.com/ipython/ipython/b=
lob/master/IPython/core/tests/test_interactiveshell.py" target=3D"_blank">h=
ttps://github.com/ipython/ipy<wbr>thon/blob/master/IPython/core/<wbr>tests/=
test_interactiveshell.py</a><div><div class=3D"m_9088155197930985685h5"><br=
><br>On Tuesday, April 10, 2018, Jacob Vanderplas &lt;<a href=3D"mailto:jak=
[email protected]" target=3D"_blank">[email protected]</a>&gt;=
 wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;bor=
der-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div><div style=
=3D"color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px;font-=
style:normal;font-weight:400;letter-spacing:normal;text-align:start;text-in=
dent:0px;text-transform:none;white-space:normal;word-spacing:0px;background=
-color:rgb(255,255,255)"><span style=3D"font-size:12.8px">I&#39;m trying to=
 create automated tests for some magic functions within a pytest framework<=
/span><span style=3D"font-size:12.8px">. I&#39;ve figured out how to do par=
t of what I need using an embedded interactive shell; briefly, I do somethi=
ng like this:</span></div><div style=3D"color:rgb(34,34,34);font-family:ari=
al,sans-serif;font-size:12.8px;font-style:normal;font-weight:400;letter-spa=
cing:normal;text-align:start;text-indent:0px;text-transform:none;white-spac=
e:normal;word-spacing:0px;background-color:rgb(255,255,255)"><br></div><div=
 style=3D"color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px=
;font-style:normal;font-weight:400;letter-spacing:normal;text-align:start;t=
ext-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;back=
ground-color:rgb(255,255,255)"><div><font face=3D"monospace, monospace">fro=
m<span>=C2=A0</span><span>IPython</span>.terminal.embed import InteractiveS=
hellEmbed</font></div><div><font face=3D"monospace, monospace">ipshell =3D =
InteractiveShellEmbed()</font></div><div><font face=3D"monospace, monospace=
">out =3D ipshell.run_cell(&quot;%my_magic_co<wbr>mmand&quot;)</font></div>=
<div><font face=3D"monospace, monospace">assert out.result =3D=3D expected_=
result</font></div></div><div style=3D"color:rgb(34,34,34);font-family:aria=
l,sans-serif;font-size:12.8px;font-style:normal;font-weight:400;letter-spac=
ing:normal;text-align:start;text-indent:0px;text-transform:none;white-space=
:normal;word-spacing:0px;background-color:rgb(255,255,255)"><font face=3D"m=
onospace, monospace"><br></font></div><div style=3D"color:rgb(34,34,34);fon=
t-family:arial,sans-serif;font-size:12.8px;font-style:normal;font-weight:40=
0;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:non=
e;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255)"><f=
ont face=3D"arial, helvetica, sans-serif">The problem I encounter is that m=
y magic function optionally uses=C2=A0</font><font face=3D"monospace, monos=
pace"><span>IPython</span>.get_ipython().use<wbr>r_ns</font><font face=3D"a=
rial, helvetica, sans-serif"><span>=C2=A0</span>to access variables in the =
user namespace, and<span>=C2=A0</span></font><font face=3D"monospace, monos=
pace"><span>IPython</span>.get_ipython()</font><font face=3D"arial, helveti=
ca, sans-serif"><span>=C2=A0</span>retu<wbr>rns<span>=C2=A0</span></font><f=
ont face=3D"monospace, monospace">None=C2=A0</font><font face=3D"arial, hel=
vetica, sans-serif">when run within an embedded shell in this way.</font></=
div><div style=3D"color:rgb(34,34,34);font-family:arial,sans-serif;font-siz=
e:12.8px;font-style:normal;font-weight:400;letter-spacing:normal;text-align=
:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:=
0px;background-color:rgb(255,255,255)"><font face=3D"arial, helvetica, sans=
-serif"><br></font></div><div style=3D"color:rgb(34,34,34);font-family:aria=
l,sans-serif;font-size:12.8px;font-style:normal;font-weight:400;letter-spac=
ing:normal;text-align:start;text-indent:0px;text-transform:none;white-space=
:normal;word-spacing:0px;background-color:rgb(255,255,255)"><font face=3D"a=
rial, helvetica, sans-serif">Is there a way to create a simple embedded she=
ll like this one that will behave as if it is an actual<span>=C2=A0</span><=
span>IPython</span><span>=C2=A0</span>shell, with<span>=C2=A0</span></font>=
<font face=3D"monospace, monospace"><span>IPython</span>.get_ipython()</fon=
t><font face=3D"arial, helvetica, sans-serif"><span>=C2=A0</span>def<wbr>in=
ed?</font></div><div style=3D"color:rgb(34,34,34);font-family:arial,sans-se=
rif;font-size:12.8px;font-style:normal;font-weight:400;letter-spacing:norma=
l;text-align:start;text-indent:0px;text-transform:none;white-space:normal;w=
ord-spacing:0px;background-color:rgb(255,255,255)"><font face=3D"arial, hel=
vetica, sans-serif"><br></font></div><div style=3D"color:rgb(34,34,34);font=
-family:arial,sans-serif;font-size:12.8px;font-style:normal;font-weight:400=
;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none=
;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255)"><fo=
nt face=3D"arial, helvetica, sans-serif">Thanks,</font></div><div style=3D"=
color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px;font-styl=
e:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent=
:0px;text-transform:none;white-space:normal;word-spacing:0px;background-col=
or:rgb(255,255,255)"><font face=3D"arial, helvetica, sans-serif"><br></font=
></div><div style=3D"color:rgb(34,34,34);font-family:arial,sans-serif;font-=
size:12.8px;font-style:normal;font-weight:400;letter-spacing:normal;text-al=
ign:start;text-indent:0px;text-transform:none;white-space:normal;word-spaci=
ng:0px;background-color:rgb(255,255,255)"><font face=3D"arial, helvetica, s=
ans-serif">=C2=A0 Jake</font></div></div>
</div>
</blockquote></div></div></div>
<br></div></div>______________________________<wbr>_________________<br>
IPython-dev mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">IPython-dev@pyt=
hon.org</a><br>
<a href=3D"https://mail.python.org/mailman/listinfo/ipython-dev" rel=3D"nor=
eferrer" target=3D"_blank">https://mail.python.org/mailma<wbr>n/listinfo/ip=
ython-dev</a><br>
<br></blockquote></div><br></div>
<br>______________________________<wbr>_________________<br>
IPython-dev mailing list<br>
<a href=3D"mailto:[email protected]">[email protected]</a><br>
<a href=3D"https://mail.python.org/mailman/listinfo/ipython-dev" rel=3D"nor=
eferrer" target=3D"_blank">https://mail.python.org/<wbr>mailman/listinfo/ip=
ython-dev</a><br>
<br></blockquote></div><br></div>

--f4f5e8075c4086e84d0569836328--

--===============0503904319789880199==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
IPython-dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/ipython-dev

--===============0503904319789880199==--