Setting up Emacs for Python development

  • Ubuntu packages to install: emacs autocutsel texinfo git mercurial (git and texinfo are required by el-get; mercurial is required to install pymacs)
  • To set the font size for just this session: press M-: and then type (set-face-attribute 'default nil :height 100) (taken from stack overflow)

(If you're using an old version of Ubuntu then you might need to use this PPA to get Emacs 24.3: Get the latest version of Emacs from this Ubuntu PPA)

Plug ins I use:

First configure package.el to use MELPA and marmalade. This is what I put in my .emacs file:

(require 'package)
(add-to-list 'package-archives
             '("melpa" . "") t)
(add-to-list 'package-archives
             '("marmalade" . "") t)
(when (< emacs-major-version 24)
  ;; For important compatibility libraries like cl-lib
  (add-to-list 'package-archives '("gnu" . "")))

This allows you to easily install all these other plug ins:

  • highlight-symbol
  • zenburn-theme
  • magit (better git support)
  • buffer move (easily move buffers around)
  • FlySpell
  • FlyCheck - "flymake done right"; just do package-install flycheck. Then do the 'verify setup' trick mentioned in the manual. I use flake8.
  • goto-last-change
  • ropemacs - refactoring, go to definition etc for python.
    • pip install rope ropemacs
    • in emacs, dopackage-install then pymacs to install from marmalade.
    • here (section "Edit ~/.emacs to use Ropemacs") are some important additions to .emacs
    • here's a quick guide to using ropemacs

Other plugins which come with Emacs but require some configuration:

Some commands

Make errors into clickable links in iPython

  • cd /usr/share/emacs/24.3/lisp/progmodes
  • sudo gunzip python.el.gz
  • sudo gedit python.el
  • Remove line-start (1+ (any " \t")) from line 1609
  • sudo emacs -batch -f batch-byte-compile *.el

Config .emacs for ipdb.

Code is adapted from

;; ipdb
;; from:
;; Highlight ipdb lines:
(defun annotate-pdb ()
  (highlight-lines-matching-regexp "import pdb")
  (highlight-lines-matching-regexp "pdb.set_trace()"))
(add-hook 'python-mode-hook 'annotate-pdb)
;; Keybinding to add breakpoint:
(defun python-add-breakpoint ()
  (insert "import ipdb; ipdb.set_trace()")
  (highlight-lines-matching-regexp "^[ ]*import ipdb; ipdb.set_trace()"))
(define-key python-mode-map (kbd "C-c C-t") 'python-add-breakpoint)

Notes for using Emacs with C++

  • FlyCheck: To get clang to work correctly on Ubuntu 13.10 for checking C++ I had to install clang-3.4 using these sources because 13.04 currently ships with clang 3.2 which gives the error: /usr/include/stdio.h:33:11: fatal error: 'stddef.h' file not found. The bug has been reported on launchpad.
  • Two candidates for autocompletion appear to be AutoComplete and company-mode.

Some plug-ins I have used in the past but no longer use:

Alternative editors

  • spyder (scientific dev environment)

Update 2nd July 2012

I've gone back to using PyDev (an Eclipse plug-in) for my Python development.  I do enjoy Emacs but PyDev is simply too pleasurable and powerful to ignore.  Plus PyDev appears to be receiving increasing amounts of development effort, in contrast to some of the Emacs tools I experimented with.   And Eclipse offers enough configuration options to mean that I can get Eclipse to feel pretty "Emacs-like".  The notes below are only about two thirds complete but they might still be useful...

So, I've settled on using Python 2.7 for the development of my disaggregation system.  I'm very happy with Python so far.  The more I learn about Python, the more excited I get; which is the oposite to the situation I experienced with MATLAB: the more I learnt about MATLAB, the more repulsed I became.

So, the next question becomes: which IDE to develop in?  I spent a few days using the Eclipse plugin PyDev but several things started to bug me; notably the fact that the interactive console doesn't respect the correct Eclipse keybindings.  Plus Eclipse requires lots of messing about with the mouse which can slow you down a bit.  So I started thinking seriously about using my old friend Emacs to develop Python in.  Pedro produced an excellent blog post describing how to install a bunch of add-ons to Emacs to make it a powerful Python IDE.  Unfortunately things have changed a bit since 2010 when that blog post was produced.  Hence this blog post is a list of modifications to Pedro's blog post to bring it up to date.  I certainly don't intend to replicate Pedro's excellent work; I'm just producing a "diff" to bring his blog post up to date. There's also some good notes on vanguard33's blog.

  1. Install iPython
  2. Install python-mode into ~/.emacs.d/ as per the instructions in the README and INSTALL files
  3. Don't bother manually putting (setq py-shell-name "ipython") into your ~/.emacs file. Instead, in emacs, type M-x customize-variable py-shell-name and change it to ipython
  4. easy_install rope ropemacs. Modify .emacs to get ropemacs to work as described in the ropemacs readme. Install Pymacs (there's a bug in the install at the time of writing; to get the install to work follow the step described in the bug report fix install of Pymacs on Python 2.7 (issue #28)). You might also need to add (setq py-load-pymacs-p 'nil) to your .emacs
  5. Magit is now hosted on github.

Useful resources

Tools I tinkered with but am not using

emacs-for-python looks like a quick and easy way to install lots of useful stuff. But I decided to install individual packages.

ipython.el:  I'm not certain if ipython.el is required or not (the latest version can be found in the ipython repository on github). There's lots of discussion about ipython.el on StackOverflow and github. There's also some discussion in the ipython editors.txt file (which I need to read). this recent comment by Andreas Röhler suggests that ipython.el should no longer be necessary. My assumption for the time being is that ipython.el is not required.


The anything project has been superceded by the helm project. You can get the files using something like cd ~/.emacs.d; git clone git:// and then read the file. There is also a helm-ipython project. HOWEVER, helm-python requires ipython.el (I submitted a bug report); and I'm fairly sure that ipython.el is now redundant.

auto-complete works pretty well. I grabbed the latest auto-complete from github. Before installing, it's necessary to also grab popul-el and fuzzy-el and put these two files in the auto-complete directory prior to installing. (It looks like the author has split fuzzy-el and popup-el from auto-complete but hasn't updated the documentation yet.) I like autocomplete to only offer help when I explicitly ask for help so I added (ac-set-trigger-key "TAB") (setq ac-auto-start nil) to my .emacs

Add new comment