Accessing get_ipython() within an embedded shell

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Accessing get_ipython() within an embedded shell

Jacob Vanderplas
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
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: Accessing get_ipython() within an embedded shell

Wes Turner
ipdb or pytest-ipdb may have a helpful example?

https://github.com/gotcha/ipdb/blob/master/ipdb/__main__.py


These all require nose:



https://github.com/ipython/ipython/blob/master/IPython/core/tests/test_interactiveshell.py

On Tuesday, April 10, 2018, Jacob Vanderplas <[hidden email]> 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
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: Accessing get_ipython() within an embedded shell

Jacob Vanderplas
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 <[hidden email]> wrote:
ipdb or pytest-ipdb may have a helpful example?

https://github.com/gotcha/ipdb/blob/master/ipdb/__main__.py


These all require nose:



https://github.com/ipython/ipython/blob/master/IPython/core/tests/test_interactiveshell.py


On Tuesday, April 10, 2018, Jacob Vanderplas <[hidden email]> 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
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev



_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: Accessing get_ipython() within an embedded shell

Thomas Kluyver-2
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 <[hidden email]> 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 <[hidden email]> wrote:
ipdb or pytest-ipdb may have a helpful example?

https://github.com/gotcha/ipdb/blob/master/ipdb/__main__.py


These all require nose:



https://github.com/ipython/ipython/blob/master/IPython/core/tests/test_interactiveshell.py


On Tuesday, April 10, 2018, Jacob Vanderplas <[hidden email]> 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
[hidden email]
https://mail.python.org/mailman/listinfo/ipython-dev



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



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