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

Bo Peng ben.bob at gmail.com
Fri Oct 15 11:20:46 CDT 2004

> 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.

This is not a problem to me since C-D (kill python) and F12 (or using
the original shortcut, to start python again) are easy enough to me.

To clear some confusion (what works and what does not), here is my test results:
1. python (any version) + python-mode.el (any official version)
without ipython: no problem.
2. ipython + updated python-mode.el + ipython.el from SciPY.org has
this buffer-switching problem.
3. (NOW) ipython + Probhu's new python-mode.el + ipython.el does not
have this problem.

I am excited that I can now enjoy the power of ipython as well as
emacs. With the attached emacs configuration, my way of working with
python script is:

1. emacs myscript.py, maxmize emacs window, C-x 3 ( split window )
2. F12  (start python/ipython )
3. start working:
  -- F9 (step through statement)
  -- F10 (submit selected region)
  -- F11 (submit the whole script)
  -- If I need to test run some command, I will use F12 to switch buffer
  -- In case python needs to be restarted, C-D and F12 again.
  -- all features of ipython is still there, in the ipython buffer

Here is the emacs code: (with Prabhu's fix)


; This file provide some convenient ways to debug your program
; More specifically, in python mode of emacs, you can use 
;     F9    to run a statement and forward to the next 
;     F10  to run selected text
;     F11  to run the whole buffer. 
;     F12  to switch between src and shell buffer. 
;          start python-shell if necessary
; This file can be freely distriuted under GPL
; Author:  Bo Peng (ben.bob at gmail.com)
; Date:    Sep, 2004 
; Last Modified: Oct, 14, 2004

(require `python-mode)

; ipython (with Prabhu updated python-mode.el )
; If you are using the usual python-mode, comment out the following
(setq py-shell-switch-buffers-on-execute nil)
(require `ipython)

(defun goto-python-shell () 
  "Go to the python command window (start it if needed)" 
  (setq current-python-script-buffer (current-buffer))

(defun goto-python-source () 
  "switch back to source window" 
  (switch-to-buffer-other-window current-python-script-buffer)

(defun py-execute-statement-and-step ()
  "select a statement, submit as a region and then step forward"
  (beginning-of-line 1)
  (let ((beg (point)))
    (py-next-statement 1) 
    ; if last statement.
    (if (= (point) beg) (end-of-line 1 ))
    (py-execute-region beg (point))

;  some key bindings
(define-key py-mode-map (quote [f9]) 'py-execute-statement-and-step)
(define-key py-mode-map (quote [f10]) `py-execute-region)
(define-key py-mode-map (quote [f11]) `py-execute-buffer)
(define-key py-mode-map (quote [f12]) `goto-python-shell)
(define-key py-shell-map (quote [f12]) `goto-python-source)



More information about the IPython-user mailing list