Re: Accessing get_ipython() within an embedded shell
Jacob Vanderplas <[email protected]> Tue, 10 Apr 2018 14:41:57 -0400
| Newsgroups | gmane.comp.python.ipython.devel |
|---|---|
| Message-ID | <CACpqBg0tQY54F5pOaVAwbXQotf2ehr2QNFkUo38KRXkmavdP7Q@mail.gmail.com> |
--===============2529551682149708998==
Content-Type: multipart/alternative; boundary="94eb2c0c3c6a91be56056982e01e"
--94eb2c0c3c6a91be56056982e01e
Content-Type: text/plain; charset="UTF-8"
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
>
>
--94eb2c0c3c6a91be56056982e01e
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Wes,<div>Thanks for the links! I found the clue I needed i=
n the pytest-ipdb link.</div><div><br></div><div>Instead of using:</div><di=
v><br></div><div><div style=3D"color:rgb(34,34,34);font-family:arial,sans-s=
erif;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-spacing:0px;tex=
t-decoration-style:initial;text-decoration-color:initial"><font face=3D"mon=
ospace, monospace">=C2=A0 from<span>=C2=A0</span><span class=3D"gmail-m_-35=
60420487135173310gmail-il"><span class=3D"gmail-il">IPython</span></span>.t=
erminal.embed import InteractiveShellEmbed</font></div><div style=3D"color:=
rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px;font-style:norm=
al;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;l=
etter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;w=
hite-space:normal;word-spacing:0px;text-decoration-style:initial;text-decor=
ation-color:initial"><font face=3D"monospace, monospace">=C2=A0 ipshell =3D=
InteractiveShellEmbed()</font></div><div style=3D"color:rgb(34,34,34);font=
-family:arial,sans-serif;font-size:12.8px;font-style:normal;font-variant-li=
gatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:norm=
al;text-align:start;text-indent:0px;text-transform:none;white-space:normal;=
word-spacing:0px;text-decoration-style:initial;text-decoration-color:initia=
l"><font face=3D"monospace, monospace"><br></font></div><div style=3D"color=
:rgb(34,34,34);font-size:12.8px;font-style:normal;font-variant-ligatures:no=
rmal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-al=
ign:start;text-indent:0px;text-transform:none;white-space:normal;word-spaci=
ng:0px;text-decoration-style:initial;text-decoration-color:initial"><font f=
ace=3D"arial, helvetica, sans-serif">I used this:</font></div><div style=3D=
"text-align:start;text-indent:0px;text-decoration-style:initial;text-decora=
tion-color:initial"><span style=3D"font-size:12.8px"><div style=3D"font-fam=
ily:arial,helvetica,sans-serif"><br></div><div style=3D""><font face=3D"mon=
ospace, monospace">=C2=A0 from IPython import InteractiveShell</font></div>=
<div style=3D""><font face=3D"monospace, monospace">=C2=A0 ipshell =3D Inte=
ractiveShell.instance()</font></div></span></div><div style=3D"color:rgb(34=
,34,34);font-size:12.8px;font-style:normal;font-variant-ligatures:normal;fo=
nt-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:sta=
rt;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"><br></font></div><div style=3D"color:rgb(34,3=
4,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-spacing:0px;te=
xt-decoration-style:initial;text-decoration-color:initial"><font face=3D"ar=
ial, helvetica, sans-serif">And now calls to IPython.get_ipython() within t=
he shell work properly.</font></div><div style=3D"color:rgb(34,34,34);font-=
size:12.8px;font-style:normal;font-variant-ligatures:normal;font-variant-ca=
ps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-inden=
t:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decorati=
on-style:initial;text-decoration-color:initial"><font face=3D"arial, helvet=
ica, sans-serif"><br></font></div><div style=3D"color:rgb(34,34,34);font-si=
ze: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-spacing:0px;text-decoration=
-style:initial;text-decoration-color:initial"><font face=3D"arial, helvetic=
a, sans-serif">Thanks,</font></div><div style=3D"color:rgb(34,34,34);font-s=
ize:12.8px;font-style:normal;font-variant-ligatures:normal;font-variant-cap=
s:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent=
:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoratio=
n-style:initial;text-decoration-color:initial"><font face=3D"arial, helveti=
ca, sans-serif">=C2=A0 =C2=A0 Jake</font></div></div></div><div class=3D"gm=
ail_extra"><br clear=3D"all"><div><div class=3D"gmail_signature" data-smart=
mail=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"lt=
r"><span style=3D"font-size:x-small">=C2=A0Director of Open Software</span>=
<div><div><font size=3D"1">=C2=A0</font><span style=3D"font-size:x-small">U=
niversity of Washington=C2=A0</span><span style=3D"font-size:x-small">eScie=
nce Institute<br></span></div></div></div></div></div></div></div></div>
<br><div class=3D"gmail_quote">On Tue, Apr 10, 2018 at 2:22 AM, Wes Turner =
<span dir=3D"ltr"><<a href=3D"mailto:[email protected]" target=3D"_bl=
ank">[email protected]</a>></span> wrote:<br><blockquote class=3D"gma=
il_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-lef=
t:1ex"><div>ipdb or pytest-ipdb may have a helpful example?</div><div><br><=
/div><a href=3D"https://github.com/gotcha/ipdb/blob/master/ipdb/__main__.py=
" target=3D"_blank">https://github.com/gotcha/<wbr>ipdb/blob/master/ipdb/__=
main__<wbr>.py</a><div><br></div><div><a href=3D"https://github.com/mverteu=
il/pytest-ipdb/blob/master/pytestipdb/ptipdb.py" target=3D"_blank">https://=
github.com/mverteuil/<wbr>pytest-ipdb/blob/master/<wbr>pytestipdb/ptipdb.py=
</a></div><div><br></div><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_terminal.py" target=3D"_blank">https://github.com/ipython/=
<wbr>ipython/blob/master/IPython/<wbr>core/tests/test_magic_<wbr>terminal.p=
y</a></div><div><br></div><div><a href=3D"https://github.com/ipython/ipytho=
n/blob/master/IPython/core/tests/test_magic_arguments.py" target=3D"_blank"=
>https://github.com/ipython/<wbr>ipython/blob/master/IPython/<wbr>core/test=
s/test_magic_<wbr>arguments.py</a></div><div><br></div><div><a href=3D"http=
s://github.com/ipython/ipython/blob/master/IPython/core/tests/test_interact=
iveshell.py" target=3D"_blank">https://github.com/ipython/<wbr>ipython/blob=
/master/IPython/<wbr>core/tests/test_<wbr>interactiveshell.py</a><div><div =
class=3D"h5"><br><br>On Tuesday, April 10, 2018, Jacob Vanderplas <<a hr=
ef=3D"mailto:[email protected]" target=3D"_blank">[email protected]=
gton.edu</a>> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margi=
n:0 0 0 .8ex;border-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-si=
ze:12.8px;font-style:normal;font-weight:400;letter-spacing:normal;text-alig=
n:start;text-indent: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 py=
test framework</span><span style=3D"font-size:12.8px">. I've figured ou=
t how to do part of what I need using an embedded interactive shell; briefl=
y, I do something like this:</span></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)"=
><br></div><div style=3D"color:rgb(34,34,34);font-family:arial,sans-serif;f=
ont-size:12.8px;font-style:normal;font-weight:400;letter-spacing:normal;tex=
t-align:start;text-indent:0px;text-transform:none;white-space:normal;word-s=
pacing:0px;background-color:rgb(255,255,255)"><div><font face=3D"monospace,=
monospace">from<span>=C2=A0</span><span>IPython</span>.terminal.embed impo=
rt InteractiveShellEmbed</font></div><div><font face=3D"monospace, monospac=
e">ipshell =3D InteractiveShellEmbed()</font></div><div><font face=3D"monos=
pace, monospace">out =3D ipshell.run_cell("%my_magic_co<wbr>mmand"=
;)</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: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)"=
><font face=3D"monospace, monospace"><br></font></div><div style=3D"color:r=
gb(34,34,34);font-family:arial,sans-serif;font-size:12.8px;font-style:norma=
l;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;te=
xt-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(=
255,255,255)"><font face=3D"arial, helvetica, sans-serif">The problem I enc=
ounter is that my magic function optionally uses=C2=A0</font><font face=3D"=
monospace, monospace"><span>IPython</span>.get_ipython().use<wbr>r_ns</font=
><font face=3D"arial, helvetica, sans-serif"><span>=C2=A0</span>to access v=
ariables in the user namespace, and<span>=C2=A0</span></font><font face=3D"=
monospace, monospace"><span>IPython</span>.get_ipython()</font><font face=
=3D"arial, helvetica, sans-serif"><span>=C2=A0</span>retu<wbr>rns<span>=C2=
=A0</span></font><font face=3D"monospace, monospace">None=C2=A0</font><font=
face=3D"arial, helvetica, 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-size:12.8px;font-style:normal;font-weight:400;letter-spacin=
g:normal;text-align:start;text-indent:0px;text-transform:none;white-space:n=
ormal;word-spacing:0px;background-color:rgb(255,255,255)"><font face=3D"ari=
al, helvetica, sans-serif"><br></font></div><div style=3D"color:rgb(34,34,3=
4);font-family:arial,sans-serif;font-size:12.8px;font-style:normal;font-wei=
ght:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transfo=
rm:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,25=
5)"><font face=3D"arial, helvetica, sans-serif">Is there a way to create a =
simple embedded shell 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()</font><font face=3D"arial, helvetica, sans-serif"><span>=C2=
=A0</span>def<wbr>ined?</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)"><fon=
t face=3D"arial, helvetica, sans-serif"><br></font></div><div style=3D"colo=
r:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px;font-style:no=
rmal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px=
;text-transform:none;white-space:normal;word-spacing:0px;background-color:r=
gb(255,255,255)"><font face=3D"arial, helvetica, sans-serif">Thanks,</font>=
</div><div style=3D"color:rgb(34,34,34);font-family:arial,sans-serif;font-s=
ize:12.8px;font-style:normal;font-weight:400;letter-spacing:normal;text-ali=
gn:start;text-indent:0px;text-transform:none;white-space:normal;word-spacin=
g:0px;background-color:rgb(255,255,255)"><font face=3D"arial, helvetica, sa=
ns-serif"><br></font></div><div style=3D"color:rgb(34,34,34);font-family:ar=
ial,sans-serif;font-size:12.8px;font-style:normal;font-weight:400;letter-sp=
acing:normal;text-align:start;text-indent:0px;text-transform:none;white-spa=
ce:normal;word-spacing:0px;background-color:rgb(255,255,255)"><font face=3D=
"arial, helvetica, sans-serif">=C2=A0 Jake</font></div></div>
</div>
</blockquote></div></div></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>
--94eb2c0c3c6a91be56056982e01e--
--===============2529551682149708998==
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
--===============2529551682149708998==--