[IPython-user] Using __IP.push()

Fernando Perez Fernando.Perez at colorado.edu
Wed Mar 23 08:58:08 CST 2005


Frédéric Mantegazza wrote:
> I'm trying to use the __IP.push() method to have a line executed by IPython,
> but it does not work. I think I don't use it in the correct way, but I can't
> find the solution. Here is the error I get:
> 
> 
>>>>__IP.push("print 4+4")
> 
> ERROR: Internal Python error in the inspect module.
> Below is the traceback from this internal error.

I think you misunderstood something.  Do you want access to push() from an 
outside program, or from within ipython itself?  If the above line is run 
within ipython itself, it will obviously produce something very bizarre, since 
ipython will try to use push() on your own call to push().  This is recursive, 
and will likely blow up.

There is a way to access push() correctly from the outside, but to make it 
easier to use, I've wrapped that functionality into a new runlines() method. 
Update CVS, and try the following.

You can use push from a _standalone_ python script in this way:

[test]> cat ippush.py
#!/usr/bin/env python

import IPython
sh = IPython.Shell.IPShell()
IP = sh.IP

IP.runlines('print "This is being run by IPython"')
IP.runlines('sc a=ls i*py')

src = """\
print "a is:\\n",a
print "run a loop over a.l"
for f in a.l:
   print f
   !wc -l $f"""
IP.runlines(src)

# The regular prompt mechanism is at work
IP.runlines("""\
3+5
print _
""")

#########################

Here is what I get running that from a system prompt:

[test]> ./ippush.py
This is being run by IPython
a is:
input.py
ippush.py
iters2.py
iters3.py
iters.py
run a loop over a.l
input.py
2 input.py
ippush.py
22 ippush.py
iters2.py
23 iters2.py
iters3.py
19 iters3.py
iters.py
18 iters.py
Out[0]: 8
8

#########################

Now, the restriction of this not working from inside ipython itself remains, 
because of the recursive problem.  However, if for some reason you do need to 
access runlines from within a running ipython, you can write a magic to do so.

Let me know if this solves your problem.

Best,

f




More information about the IPython-user mailing list