[IPython-user] ipython + emacs py-execute-region problem.

Prabhu Ramachandran PrabhuRamachandran
Fri Oct 15 02:25:29 CDT 2004

Hi Bo,

Let me just introduce the problem.  

IIUC, Bo has a working setup of Emacs + vanilla Python where he
executes the current buffer or parts of the buffer or even steps
through the code using different special keys to do the job.  Moving
to IPython has been problematic because of this peculiar problem.
When he wants to step thru the buffer he is forced to switch buffers.

Since our resident Emacs guru is out on vacation I thought it might be
interesting to see how other Python users debug their code.

I'll start with my story.  I usually simply edit my code from XEmacs
and have a terminal (rxvt) running IPython.  I cd to the directory
where my code is and then use either @run script or more often do

 In [1]: import script
 In [2]: [... use script ...]

If the script fails I edit the file inside emacs and then

 In [3]: reload(script)
 In [4]: [... use script ...]

If @run will do, I simply re-'run' the script.  I seldom need to step
through my code and have possibly used pdb 10 times in the last 5
years and all of that was when I was debugging someone else's code.

Now Bo mentioned to me that running the whole script again seemed like
a pain but when I use Python interactively, this is mostly a non-issue
since there is hardly a performance hit, unless the script is doing
something highly computationally intensive.  In this case I always
make the test case simple enough that it is easy to re-run it.  Also,
executing a region from Emacs via python-mode is already incurring a
fair performance hit since IIUC, python-mode basically grabs the
region, dumps a file in /tmp and runs execfile on that region.  This
is precisely what IPython's run effectively does.  Switching between
windows is a pain but fvwm and key combinations are handy, embedding
Python into emacs of course elminates the need for a seperate window.

As regards stepping through code, usually, once I know where the error
is, its fairly straight-forward to figure out what is wrong by looking
at the traceback and running a few simple tests by importing the
library and poking at the values.  Being able to do this from emacs
perhaps is a good thing but I've seldom needed to do it.  

My guess is that by executing selected parts of the buffer Bo uses
*Emacs as if it were pdb by sending parts of the buffer to the
interpreter and then catching errors and actually inspecting them in
the py-shell when the script is alive.  Then one can quickly edit the
python code to fix things and retry.  That looks like a very
interesting way to do things.  I guess, if I do start using this
feature I'll be swearing by it too.  So, thanks Bo! :)

The only trouble with this approach is that it will not reload other
modules automatically so one will need to kill the interpreter every
so often.  When using IPython seperately, I simple have to reload the
necessary modules.  However, this is a fascinating way to debug Python
code and one I might use later.

What do the rest of you do to debug Python programs (if you ever have
to :-D ) ?


>>>>> "BP" == Bo Peng <ben.bob at gmail.com> writes:

    BP> Dear list, I am trying to use emacs + ipython but encounter
    BP> one small problem: When I submit a region of code to python
    BP> shell, current buffer will always change to the python
    BP> buffer. This is very inconvenient since I have to switch
    BP> buffer before I submit another part of the script. (I am using
    BP> a shortcut to step through my script).


More information about the IPython-user mailing list