Tricked out emacs for python coding

Various ways to configure your emacs that you might find useful.

See emacs_python_mode for a good summary.

ReST mode

For editing ReST documents like this one. You may need a recent version of the rst.el file from the docutils site.

rst mode automates many important ReST tasks like building and updating table-of-contents, and promoting or demoting section headings. Here is the basic .emacs configuration:

(require 'rst)
(setq auto-mode-alist
      (append '(("\\.txt$" . rst-mode)
                ("\\.rst$" . rst-mode)
                ("\\.rest$" . rst-mode)) auto-mode-alist))

Some helpful functions:

C-c TAB - rst-toc-insert

  Insert table of contents at point

C-c C-u - rst-toc-update

    Update the table of contents at point

C-c C-l rst-shift-region-left

    Shift region to the left

C-c C-r rst-shift-region-right

    Shift region to the right


On older Debian-based releases, the default M-x rst-compile command uses whereas the command installed is rst2html. Symlink was required as a quick fix.

doctest mode

This useful mode for writing doctests (doctest-mode.el) cames with python-mode package on Debian-based systems. Otherwise see doctest-mode project page.

code checkers

Code checkers within emacs can be useful to check code for errors, unused variables, imports and so on. Alternatives are pychecker, pylint and pyflakes. Note that rope (below) also does some code checking. pylint and pyflakes work best with emacs flymake, which usually comes with emacs.


This appears to be plumbed in with python-mode, just do M-x py-pychecker-run. If you try this, and pychecker is not installed, you will get an error. You can install it using your package manager (pychecker on Debian-based systems) or from the pychecker webpage.


Install pylint. Debian packages pylint as pylint. Put the flymake .emacs snippet in your .emacs file. You will see, in the emacs_python_mode page, that you will need to save this:

#!/usr/bin/env python

import re
import sys

from subprocess import *

p = Popen("pylint -f parseable -r n --disable-msg-cat=C,R %s" %
          sys.argv[1], shell = True, stdout = PIPE).stdout

for line in p.readlines():
    match ="\\[([WE])(, (.+?))?\\]", line)
    if match:
        kind =
        func =

        if kind == "W":
           msg = "Warning"
           msg = "Error"

        if func:
            line = re.sub("\\[([WE])(, (.+?))?\\]",
                          "%s (%s):" % (msg, func), line)
            line = re.sub("\\[([WE])?\\]", "%s:" % msg, line)
    print line,


as epylint somewhere on your system path, and test that epylint works.


Install pyflakes. Maybe your package manager again? (apt-get install pyflakes). Install the flymake .emacs snippet in your .emacs file.

flymake .emacs snippet

Add this to your .emacs file:

;; code checking via flymake
;; set code checker here from "epylint", "pyflakes"
(setq pycodechecker "pyflakes")
(when (load "flymake" t)
  (defun flymake-pycodecheck-init ()
    (let* ((temp-file (flymake-init-create-temp-buffer-copy
           (local-file (file-relative-name
                        (file-name-directory buffer-file-name))))
      (list pycodechecker (list local-file))))
  (add-to-list 'flymake-allowed-file-name-masks
               '("\\.py\\'" flymake-pycodecheck-init)))

and set which of pylint (“epylint”) or pyflakes (“pyflakes”) you want to use.

You may also consider using the flymake-cursor functions, see the pyflakes section of the emacs_python_mode page for details.


rope is a python refactoring library, and ropemacs is an emacs interface to it, that uses pymacs. pymacs is an interface between emacs lisp and python that allows emacs to call into python and python to call back into emacs.


  • rope - by downloading from the link, and running python install in the usual way.
  • pymacs - probably via your package manager - for example apt-get install pymacs
  • ropemacs - download from link, python install

You may need to make sure your gnome etc sessions have the correct python path settings - for example settings in .gnomerc as well as the usual .bashrc.

Make sure you can import ropemacs from python (which should drop you into something lispey). Add these lines somewhere in your .emacs file:

(require 'pymacs)
(pymacs-load "ropemacs" "rope-")

and restart emacs. When you open a python file, you should have a rope menu. Note C-c g - the excellent goto-definition command.

Switching between modes

You may well find it useful to be able to switch fluidly between python mode, doctest mode, ReST mode and flymake mode (pylint). You can attach these modes to function keys in your .emacs file with something like:

(global-set-key [f8]      'flymake-mode)
(global-set-key [f9]      'python-mode)
(global-set-key [f10]      'doctest-mode)
(global-set-key [f11]      'rst-mode)

emacs code browser

Not really python specific, but a rather nice set of windows for browsing code directories, and code - see the ECB page. Again, your package manager may help you (apt-get install ecb).