Python EMACS Extensions

This package contains three extensions to python-mode which I find useful when programming python. They are:

They are packaged separately so that one may choose which to use. I have used the first two for some time and find that they are very useful. The pycomplete enhancement also seems useful but I have not been using it long.

I have successfully used these extensions on both Emacs-21 and Emacs-22.

py-mode-ext

I really like to run and debugging python programs from inside of emacs but I have wanted to see a couple of additional features added to python-mode.el This applies to executing the program as well as running with a symbolic debugger.

py-mode-ext.el is a short bit of emacs lisp which I assert accomplishes the above. I am new to writing elisp so I welcome comments and suggestions.

To use these extensions, py-mode-ext.el is placed in my emacs load path. See below for the code that I inserted into my .emacs file.

When the user invokes py-execute-prog or py-call-pdb, the following sequence occurs.

  1. It first reads from the mini-buffer the name of the initial file to be executed by the interpreter. If no history exists, then the file behind the current buffer is suggested. The user may then substitute any file he wishes as the initial file to be executed by the interpreter. It will be save in a history list.

  2. Next, it reads from the mini-buffer the argument string to be passed to the program being interpreted. Again, if there is a history of arguments, the most recent will be selected. The user may over ride the suggestion. The argument string passed to the program will be saved in a history list.

  3. It next saves all the modified buffers based on queries.

  4. Finally, it invokes python on the initial file or it invokes pdb on the initial file.

Traceback Navigation

py-mode-ext.el also includes several functions for moving up and down the trace back stack when a error occurs. The following commands are implemented.

You may invoke these functions with the following key sequences:

Start with \C-x\C-l. Then move up or down the stack.

The easist way to navigation an error trace when executing a program is to use mouse-3. [mouse-3] is defined to invoke 'py-current-line-exception. Just go to the *Python Output* window, select the traceback line you want to visit with mouse-1 and push the mouse-3 button. This is the variation that I use most often.

Since I have been using the traceback navigation above and pyp to easily insert debugging statements, I have not used the debugger very often.

Invoking Pychecker

When the user initiates the command for pychecker, it will check whether any buffers have been changed and ask whether you want any of those saved. Then it will run PyChecker against the file behind the current buffer.

pyp

pyp is a pair of functions which facilitate adding print statements to a python module. They are:

I attach pyp to F12 so I just have to enter the expression and hit return to get my debugging output. Using 'list-matching-lines' or grep with 'pyp' give me an easy way to remove the statements.

pycomplete

I have included an updated version of pycomplete.

This is a nifty emacs extension which helps one to insert attributes and functions into a python program. For instance, one can type

os.path.is

then ESC-Tab opens a window showing all attributes and function in os.path which begin with "is". Also, functions are can be distinguished from simple attributes because they are followed by "()".

Pick one with the mouse and hit and the completion will take place. It is required that there be an appropriate include statement, "os.path" in the example, in order for the completion to work. If it is not included one get a message the name is undefined.

I found an early version which was rather limited. Looking around the net, I found reference to an extension patch. When I applied the patch, I agreed that it was better. So I removed some unnecessary code from the package and added the facility to distinguish functions and to end up in the python buffer after the completion. There are two pieces to the package - pycomplete.el which is loaded by emacs so must be in the emacs load path and pycomplete.py which should go into an active site-packages directory or anywhere else in the PYTHONPATH.

Download

This collection of extensions may be downloaded from Summary Page.

Installaton

The python mode extensions are installed by placing "py-mode-ext.el" in the emacs load path specified in the emacs load path. See below for the code I use in my .emacs to activate the extensions.

To set up GUD, the emacs debugger for use with my python mode extensions, set the emacs option gud-pdbpcommand-name to point to the file "pdb.py". In my .emacs I have the statement:

(gud-pdb-command-name "~/pdb.py")
Of course, I find that pdb.py is now located in /usr/lib/python2.5 and that location could be used in the .emacs statement above. The setting can be made via the Options menu.

To use pyp just move pyp.el to the emacs load path and see the .emacs code below for attaching it to a function key.

Installation of pycomplete is slightly more complicated because it requires pymacs which needs to have been installed. For Debian based systems you can probably use apt-get. Otherwise, get a tarball and install it from there.

I think that pymacs is really great but at present it does not appear to be supported and the documentation is poor like many Open Source programs. The main documentation problem I found was a paucity of examples. For instance, I fussed around for a while before I discovered that the output of print statements show up in the *Pymacs* buffer. For some time, I was unable to find a website for pymacs. Recently, I found a site devoted to Pymacs; it provides downloads and documentation. This site has documentation labeled Pymacs version 0.23 but the latest packaged version on the site is version 0.22.

Fortunately, pymacs is easier to install than to find. Instructions are in the package and are pretty standard with nothing to compile. setup.py does everything without having to compiling anything. When I tried testing pycomlete under emacs-22, I found that merely moving pymacs.el from the distribution directory to the Emacs load path is sufficient.

Installation of pycomplete consists of merely moving pycomplete.el to the emacs load path and pycomplete.py to the PYTHONPATH. Again look at the .emacs code below.

.Emacs code.

I have the following code in my .emacs to enable these extensions.

(add-hook 'python-mode-hook
       	  '(lambda ()
             (load "py-mode-ext")  
             (load "pyp")
             (require 'pycomplete)
             (define-key py-mode-map [f12] 'pyp)
             (define-key py-mode-map "\C-c\C-c" 'py-execute-prog)
             (define-key py-mode-map "\C-c\C-g" 'py-call-pdb)
             (define-key py-mode-map "\C-c\C-w" 'pychecker)))

You may contact me at: