SaltyCrane Blog — Notes on JavaScript and web development

My Emacs Python environment

Python mode

There are two Python modes: python-mode.el created by the Python community and python.el created by the Emacs community (David Love). The first version of python.el was included in Emacs 22 and an improved version was included in Emacs 23. I am currently using the default python.el included with Emacs 23. Below are some useful commands. For a complete list, open a Python file and run M-x apropos RET python RET

python-shift-left, C-c C-<
Decrease indentation of the region
python-shift-right, C-c C-<
Increase indentation of the region
python-switch-to-python, C-c C-z
Start (or switch) to a Python shell
python-send-buffer, C-c C-c
Run the current buffer in the Python interpreter
python-send-region, C-c C-r
Run the selected code in the Python interpreter
python-describe-symbol, C-c C-f
Get help on a Python symbol (Better than visiting the slow Python website, right?)
Configuration (~/.emacs changes)
(setq-default indent-tabs-mode nil)    ; use only spaces and no tabs
(setq default-tab-width 4)

Ropemacs

Ropemacs is an Emacs plugin to use Rope, a powerful Python refactoring library. It supports code completion, renaming, and other refactoring. Pymacs is a prerequisite for Ropemacs. Below are some useful commands. For more information, look in the Rope menu.

rope-code-assist, M-/
Code completion
rope-rename, C-c r r
Rename a variable, function, etc.
Installation

Note: pip install does not work with v0.25. Need to use `make install`

  • Install Pymacs (Emacs part)
    $ curl -L https://github.com/pinard/Pymacs/tarball/v0.24-beta2 | tar zx
    $ cd pinard-Pymacs-016b0bc
    $ make 
    $ mkdir -p ~/.emacs.d/vendor/pymacs-0.24-beta2
    $ cp pymacs.el ~/.emacs.d/vendor/pymacs-0.24-beta2/pymacs.el 
    $ emacs -batch -eval '(byte-compile-file "~/.emacs.d/vendor/pymacs-0.24-beta2/pymacs.el")' 
    
  • Install Pymacs (Python part)
    $ sudo pip install https://github.com/pinard/Pymacs/tarball/v0.24-beta2 
    
  • Install Ropemacs and Rope
    $ sudo pip install http://bitbucket.org/agr/ropemacs/get/tip.tar.gz 
    
  • Edit ~/.emacs to use Ropemacs
    (add-to-list 'load-path "~/.emacs.d/vendor/pymacs-0.24-beta2")
    (require 'pymacs)
    (pymacs-load "ropemacs" "rope-")
    (setq ropemacs-enable-autoimport t)

Auto Complete Mode

Auto Complete Mode is an extension to make Emacs auto-completion nicer. It creates a drowpdown in the middle of your text. It can use Ropemacs as a backend.

  • Download, unpack and byte-compile
    $ cd ~/.emacs.d/vendor 
    $ curl http://cx4a.org/pub/auto-complete/auto-complete-1.2.tar.bz2 | tar jx 
    $ cd auto-complete-1.2 
    $ make byte-compile 
    
  • Edit ~/.emacs
    (add-to-list 'load-path "~/.emacs.d/vendor/auto-complete-1.2")
    (require 'auto-complete-config)
    (add-to-list 'ac-dictionary-directories "~/.emacs.d/vendor/auto-complete-1.2/dict")
    (ac-config-default)

Yasnippet

YASnippet is a template system inspired by Textmate. Demo on YouTube

Code checking option 1: On-the-fly w/ Flymake

Flymake is part of Emacs 23. PyFlakes. pep8. See also pylint. Reference. Note: flymake-cursor.el is not required, but it makes flymake less obtrusive.
  • Install PyFlakes and pep8
    $ sudo pip install pyflakes pep8
    
  • Get flymake-cursor.el and save it as ~/.emacs.d/vendor/flymake-cursor.el.
  • Edit ~/.emacs:
    (add-to-list 'load-path "~/.emacs.d/vendor")
    
    (add-hook 'find-file-hook 'flymake-find-file-hook)
    (when (load "flymake" t)
      (defun flymake-pyflakes-init ()
        (let* ((temp-file (flymake-init-create-temp-buffer-copy
                   'flymake-create-temp-inplace))
           (local-file (file-relative-name
                temp-file
                (file-name-directory buffer-file-name))))
          (list "pycheckers"  (list local-file))))
       (add-to-list 'flymake-allowed-file-name-masks
                 '("\\.py\\'" flymake-pyflakes-init)))
    (load-library "flymake-cursor")
    (global-set-key [f10] 'flymake-goto-prev-error)
    (global-set-key [f11] 'flymake-goto-next-error)
  • Create pycheckers, make it executable, and put it on your PATH
    #!/bin/bash
    
    pyflakes "$1"
    pep8 --ignore=E221,E701,E202 --repeat "$1"
    true

Code checking option 2: Manual checking w/ python-check

PyFlakes is a Lint-like tool for Python. It is like PyChecker, but it is fast. PyFlakes can be run from within Emacs using the default python mode in Emacs23.

  • Install PyFlakes
    $ sudo pip install pyflakes
    
  • Edit ~/.emacs
    (setq python-check-command "pyflakes")
  • From Emacs, while visiting a Python file, run M-x python-check or C-c C-v or C-c C-w.

Django

Reference

  • $ wget http://ourcomments.org/Emacs/DL/elisp/nxhtml/zip/nxhtml-2.08-100425.zip 
    $ unzip nxhtml-2.08-100425.zip
        
    
  • (load "~/.emacs.d/nxhtml/autostart.el")
    (setq mumamo-background-colors nil) 
    (add-to-list 'auto-mode-alist '("\\.html$" . django-html-mumamo-mode))
        

References

See also

Comments


#1 Jake commented on :

This has been the best written (and up to date using current packages!) page I've seen so far for ropemacs, python, and emacs.

Thanks so much.


#2 K commented on :

Very concise and helpful to take python and emacs to the next level, thank you. Just a note too that Auto Complete mode is now at v1.3 instead of 1.2.


#3 Eliot commented on :

K: Great, I will have to update to Auto Complete 1.3


#4 Anders Rønningen commented on :

Works great on my system, except for two things.

  1. Every time I edit a C-file, a warning pops up that flymake is disabled. I'm guessing either load flymake only for python files, or disable warnings.

  2. Both warnings and errors use the flymake-errline face. Since the messages most often contain "W:" and "E:", it should be possible to fix this somewhere.

I'll dig more into this, but I posted in case you already have the answers.

Thanks for the setup :)


#5 Anders Rønningen commented on :
  1. was of course trivial:

(defun anr78/flymake-hook () ; your flymake-stuff here )

(add-hook 'python-mode-hook 'anr78/flymake-hook)


#6 Eliot commented on :

Anders: Thanks for your comments. I will have to look into #2. I would like to fix this also. Let me know if you figure it out.


#7 Anders Rønningen commented on :

Using pylint instead of pyflakes, I have the colors working. The pylint-checker looks like:

#!/usr/bin/env python

import re
import sys
from subprocess import *

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

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

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

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

#8 Eliot commented on :

Anders: Thanks for the code.


#9 ygneo commented on :

Finally I did python autocompletion as I wanted!! Thanks a lot!


#10 Marcio Mazza commented on :

Anders: Since the option --disable doesn't seem to exist anymore I switched the command line to

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

#11 JU commented on :

Hi, thanks for the code. But one problem is my yasnippet doesn't work...


#12 Tu commented on :

Thanks a lot. Getting lots of troubles with the other manuals (I'm an Emacs newbie). This guide is concise, easy to follow and works like charm.


#13 simplynitaai commented on :

Hi

Thanks for the tutorial.

i am having the following error when starting emacs:

"error: Pymacs helper did not start within 30 seconds"

It seems i forgot something... Any help would be appreciated.

i follow your steps up to yasnippet.

Thanks simplynitaai


#14 AlexS commented on :

Emacs VS

Post some plugins on : Emacs VS

Thanks !


#15 Ricopan commented on :

This was really great. Unfortunately I've now moved to python 3 so ... yuck. Still, any advice on what can be salvaged? Found pymacs and rope for py3k but no ropemode, ropemacs. Autocomplete for py3k apparently here http://www.rwdev.eu/articles/emacspyeng.