[IPython-dev] embedding ipython, namespace question

Darren Dale dsdale24 at gmail.com
Sat Jan 3 10:45:41 EST 2015


Hi Ray,

On Sat Dec 27 2014 at 8:48:20 PM Osborn, Raymond <rosborn at anl.gov> wrote:

>  I don’t really understand what you are trying to achieve, but the
> ‘user_ns’ dictionary isn’t an isolated namespace - it’s the namespace that
> is used by the console, and I would have thought you would have to do some
> kind of injection to add other objects from within the application.
>

What I am trying to achieve is explicitly documented at
http://ipython.org/ipython-doc/dev/interactive/reference.html#embedding-ipython
:

---
 It is also possible to embed an IPython shell in a namespace in your
Python code. This allows you to evaluate dynamically the state of your
code, operate with your variables, analyze them, etc. Note however that any
changes you make to values while in the shell do not propagate back to the
running code, so it is safe to modify your values because you won’t break
your code in bizarre ways by doing so.

Note
At present, embedding IPython cannot be done from inside IPython. Run the
code samples below outside IPython.
[DD: I am not attempting to embed ipython from inside ipython]

This feature allows you to easily have a fully functional python
environment for doing object introspection anywhere in your code with a
simple function call. In some cases a simple print statement is enough, but
if you need to do more detailed analysis of a code fragment this feature
can be very valuable.

It can also be useful in scientific computing situations where it is common
to need to do some automatic, computationally intensive part and then stop
to look at data, plots, etc. Opening an IPython instance will give you full
access to your data and functions, and you can resume program execution
once you are done with the interactive part (perhaps to stop again later,
as many times as needed).
The following code snippet is the bare minimum you need to include in your
Python programs for this to work (detailed examples follow later):

   from IPython import embed
   embed() # this call anywhere in your program will start IPython

You can also embed an IPython kernel, for use with qtconsole, etc. via
IPython.embed_kernel(). This should function work the same way, but you can
connect an external frontend (ipython qtconsole or ipython console), rather
than interacting with it in the terminal.
---

This is impressively simple for the embed function:

---
C:\Users\darren> python
Python 2.7.8 |Continuum Analytics, Inc.| (default, Jul  2 2014, 15:12:11)
[MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>> from IPython import embed
>>> a=1
>>> embed()
Python 2.7.8 |Continuum Analytics, Inc.| (default, Jul  2 2014, 15:12:11)
[MSC v.1500 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 2.3.1 -- An enhanced Interactive Python.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: a
Out[1]: 1
---

But I have not been able to achieve the same behavior with the qt
in-process console.

Darren
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20150103/9e2d2fd7/attachment.html>


More information about the IPython-dev mailing list