[IPython-user] help with embedding
David M. Kaplan
dmk@ucsc....
Tue Jul 8 10:31:12 CDT 2008
Hi,
I am starting to use ipython as a replacement for matlab. I have been
trying to use IPShellEmbed to create a "keyboard-like" command for
ipython and have some problems/questions. keyboard is a command in
matlab that you stick in a script that allows you to drop out of the
script and into a manual entry state where you have access to the same
namespace as the script and can look at and modify variables. As I
understand, this is the purpose of IPShellEmbed (except for modifying
variables, which I don't think it allows - correct me if I am wrong).
However, I can't seem to get it quite right. I think I have also found
some bugs, or at the very least some very strange behavior.
First I generate an IPShellEmbed instance:
from IPython.Shell import IPShellEmbed
keyboard = IPShellEmbed(['-pi1','keyboard <\\#>'], \
'Entering Keyboard','Exiting Keyboard')
Then I use it with keyboard(). This basically works, but I have the
following questions:
1) Calling keyboard() from ipython itself seems to drop me into a new
namespace that doesn't know anything about the variables in the original
ipython namespace. I can fix this by setting the local or global
namespace to _ip.user_ns. Can someone explain why this is necessary?
Naively, I would expect to end up in the ipython namespace just as
calling keyboard() from a script does.
2) The restriction to not being able to modify variables is a bit of a
pain. In matlab, I often use keyboard to give information to my
scripts. Is there a way around this? Or is there another approach I
should be using?
3) When I create the shell instance, it spits out information about the
logging state, as if I had just started ipython. This makes some sense
as it is like starting a new ipython, but this has a strange interaction
with the ipython history. All the commands I enter end up in
ipython_log.py, but when I create the instance from inside ipython
itself, it has the effect of making ipython "forget" about previous
commands. If I use the up arrow key to flip through previous commands,
all commands from the current session are absent, including the command
I used to create the embedded ipython instance. Is this normal?
3) Once I finish in the embedded shell, I enter %quit and leave it.
After this, attempting to reuse the keyboard() immediately exits the
shell. Presumably it is dead. Is there a way to make an embedded shell
that stays alive or to bring back to life a dead shell? Or do I need to
create a new instance each time?
4) If I create, use and exit an embedded shell, either from ipython
itself or in a script I execute with %run (and possibly other ways), and
then I try to use the %quit magic command to exit ipython, it does
nothing. It asks if I want to leave, I enter y, hit enter and then it
just puts me right back in ipython. The only way I have found to exit
is with ctrl-d. This seems like a bug to me. Am I correct?
Thanks,
David
P.S. Below is a recent ipython session indicating some of the issues
discussed above.
--------------------------
$ ipython
Activating auto-logging. Current session state plus future input saved.
Filename : ipython_log.py
Mode : append
Output logging : False
Raw input log : False
Timestamping : False
State : active
Python 2.5.2 (r252:60911, Apr 21 2008, 11:12:42)
Type "copyright", "credits" or "license" for more information.
IPython 0.8.1 -- An enhanced Interactive Python.
? -> Introduction to IPython's features.
%magic -> Information about IPython's 'magic' % functions.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
In [1]: from IPython.Shell import IPShellEmbed
In [2]:
In [2]: keyboard = IPShellEmbed(['-pi1','keyboard <\\#>'],'Entering
Keyoard','Exiting Keyboard')
Activating auto-logging. Current session state plus future input saved.
Filename : ipython_log.py
Mode : append
Output logging : False
Raw input log : False
Timestamping : False
State : active
In [3]: a=1
In [4]: a
Out[4]: 1
In [5]: keyboard()
Entering Keyoard
keyboard <1>a
---------------------------------------------------------------------------
<type 'exceptions.IndexError'> Traceback (most recent call
last)
/home/dmk/docs/python/adult_movement/<ipython console> in <module>()
/var/lib/python-support/python2.5/IPython/Prompts.py in __call__(self,
arg)
505
506 # do not print output if input ends in ';'
--> 507 if self.input_hist[self.prompt_count].endswith(';
\n'):
508 return
509 # don't use print, puts an extra space
<type 'exceptions.IndexError'>: list index out of range
keyboard <7>b=1
keyboard <8>%quit
Do you really want to exit ([y]/n)? y
Exiting Keyboard
In [6]: b
---------------------------------------------------------------------------
<type 'exceptions.NameError'> Traceback (most recent call
last)
/home/dmk/docs/python/adult_movement/<ipython console> in <module>()
<type 'exceptions.NameError'>: name 'b' is not defined
In [10]: a
Out[10]: 1
In [11]: keyboard()
Exiting Keyboard
In [12]: %quit
Do you really want to exit ([y]/n)? y
In [13]: %quit
Do you really want to exit ([y]/n)? y
In [14]: %quit
Do you really want to exit ([y]/n)? y
In [15]: %quit
Do you really want to exit ([y]/n)? y
In [16]:
Do you really want to exit ([y]/n)? y
$
--
**********************************
David M. Kaplan
Charge de Recherche 1
Institut de Recherche pour le Developpement
Centre de Recherche Halieutique Mediterraneenne et Tropicale
av. Jean Monnet
B.P. 171
34203 Sete cedex
France
Phone: +33 (0)4 99 57 32 27
Fax: +33 (0)4 99 57 32 95
http://www.ur097.ird.fr/team/dkaplan/index.html
**********************************
More information about the IPython-user
mailing list