Emacs and Python

Question:

I recently started learning Emacs. I went through the tutorial, read some introductory articles, so far so good.

Now I want to use it for Python development. From what I understand, there are two separate Python modes for Emacs: python-mode.el, which is part of the Python project; and python.el, which is part of Emacs 22.

I read all information I could find but most of it seems fairly outdated and I’m still confused.

The questions:

  1. What is their difference?
  2. Which mode should I install and use?
  3. Are there other Emacs add-ons that are essential for Python development?

Relevant links:

Answers:

If you are using GNU Emacs 21 or before, or XEmacs, use python-mode.el. The GNU Emacs 22 python.el won’t work on them. On GNU Emacs 22, python.el does work, and ties in better with GNU Emacs’s own symbol parsing and completion, ElDoc, etc. I use XEmacs myself, so I don’t use it, and I have heard people complain that it didn’t work very nicely in the past, but there are updates available that fix some of the issues (for instance, on the emacswiki page you link), and you would hope some were integrated upstream by now. If I were the GNU Emacs kind, I would use python.el until I found specific reasons not to.

The python-mode.el’s single biggest problem as far as I’ve seen is that it doesn’t quite understand triple-quoted strings. It treats them as single-quoted, meaning that a single quote inside a triple-quoted string will throw off the syntax highlighting: it’ll think the string has ended there. You may also need to change your auto-mode-alist to turn on python-mode for .py files; I don’t remember if that’s still the case but my init.el has been setting auto-mode-alist for many years now.

As for other addons, nothing I would consider ‘essential’. XEmacs’s func-menu is sometimes useful, it gives you a little function/class browser menu for the current file. I don’t remember if GNU Emacs has anything similar. I have a rst-mode for reStructuredText editing, as that’s used in some projects. Tying into whatever VC you use, if any, may be useful to you, but there is builtin support for most and easily downloaded .el files for the others.

Answered By: Thomas Wouters

This site has a description of how to get Python code completion in Emacs.
Ropemacs is a way to get Rope to work in emacs. I haven’t had extensive experience with either, but they’re worth looking into.

Answered By: James Sulak

Given the number of times I have several open buffers all called __init__.py, I consider the uniquify library essential for python development.

Pyflakes also aids productivity.

Answered By: seb
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.