aliquote.org

Emacs auto-completion for Python

February 11, 2013

Here are some notes on how to enable auto-completion for Emacs.

I already have auto-complete installed and enabled for some major modes. Together with other goodies from ess or yasnippet it really makes life easier when working with R code.

Following a recent post by John D Cook, I decided to try to enhance my default Emacs setup for Python (which barely consists in python-mode, ac-python and some custom hooks for indentation and tabs/spaces management). I should note that I am currently using standard Python shell as inferior process because I wasn’t able to configure Emacs to run ipython instead of standard Python. This may well be due to conflicting version of python-mode and ipython.el. With ipython.el from IPython distro, and the following in my .emacs :

(require 'ipython)
(setq ipython-command "/usr/local/bin/ipython")
(setq py-python-command-args '("-pylab" "-colors" "LightBG"))

I seem to be able to send Python code to IPython, though.

I already know of ropemacs but it looks too much for what I really need. Here comes Jedi.el:

a Python auto-completion package for Emacs. It aims at helping your Python coding in a non-destructive way. It also helps you to find information about Python objects, such as docstring, function arguments and code location.

Installation instructions are dead simple. However, instead of using el-get, I installed everything by hand.

For the Python part, we just need to install epc and jedi:

% sudo pip install epc
% sudo pip install jedi

(I’m still using Apple Python, which actually is Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05).)

And, for Emacs, provided we are using package.el, we just have to M-x package-list-packages and then install epc and jedi. My package archives include http://melpa.milkbox.net/packages/ which generally provides more up to date packages. I added the following to my .emacs:

(require 'epc)
(setq jedi:setup-keys t)
(setq jedi:server-command '("python" "~/.emacs.d/elpa/jedi-20130210.1518/jediepcserver.py"))
(setq jedi:tooltip-method '(pos-tip))
(autoload 'jedi:setup "jedi" nil t)
(add-hook 'python-mode-hook 'jedi:setup)

Below are some screenshots of jedi.el in action:

Jedi

On a related point, I took advantage of those updates to update my nrepl package for Clojure and install ac-nrepl which also comes with handy autocompletion and doc (C-c C-d) support.

Clojure and nREPL

See Also

» Interacting with Weka from Jython » GNU Emacs on OS X 10.7 » Color schemes for Emacs and TeX » IPython 0.11, the new killer app » Installing numpy+scipy on OS X Lion