This tutorial will show you how to setup a very convenient python IDE in Emacs. Many ideas in this tutorial came from emacs python wiki and Ryan McGuire Homepage . Many people on #emacs channel of freenode.net provided invaluable help. Freenode is one of the best communities I’ve ever known. It really takes time to combine all kinds of modes together and make them work. so I hope this tutorial could save you some time.
Requirments:
==========================
Software Version:Ubuntu 8.04 Emacs 22.1
Package required: python-mode.el , pymacs0.24 , auto-complete.el, yasnippet.el, rope and ropemacs
From my experience, many emacs packages in Ubuntu repository are out of data. So you’d better download emacs packages directly from Emacs wiki or the homepage of that mode. It not only keeps you from compatibility issues but also gives you extra portability when you move to another machine. Let’s see some details of the final results. Hopefully, these will give you enough anticipation to finish this tutorial.
Features
==========================

Auto completion by auto-complete.el
1. Auto complete variable/ function names within a file. It’s really fast!

Code completion by Rope
2. Complete names in other files or python libraries.
3. code refectoring for more detail

Code expansion by yasnippets
4. templates expansion including class, function, file header and almost anything you want. I use it to generate stander file header and if __name__==”__main__” staff.

Display docstring by rope
5. Online help system.
6. Syntax checking on the fly
Other features are:
Smart Indent, Outdent, and Pair matching, additional to syntactic and semantic highlighting, code folding, instant rename refactoring, mark occurrences
Interactive Python Console
Smart math operator, Add space around operator such as =.+,-,*./ for better readability
…
How to install:
==========================
I assume that you have a .emacs file and .emacs.d directory in your home directory. If you don’t, create them and put this in your .emacs
(add-to-list ‘load-path “~/.emacs.d/”)
Dotemacs is a very good site for newbies. If you dont’ have .emacs, you want to go there and look for some basic settings
==========================
1.Download auto-completion.el to .emacs.d and put the following line in .emacs
2. Download yasnippet to .emacs.d and edit .emacs
(require ‘yasnippet)
(yas/initialize)
(yas/load-directory “~/.emacs.d/snippets”)
3. Download python-mode.el and put it in .emacs.d, we will use it later.
4. Setup Rope, Ropemacs and Pymacs.
We need the latest development version of Rope and Ropemacs. Otherwise, emacs can not find the rope-completions function.
I just copy’n paste from Ryan’s website. You can find the original post here
sudo apt-get install mercurial
mkdir /tmp/rope && cd /tmp/rope
hg clone http://bitbucket.org/agr/rope
hg clone http://bitbucket.org/agr/ropemacs
hg clone http://bitbucket.org/agr/ropemode
sudo easy_install rope
ln -s ../ropemode/ropemode ropemacs/
sudo easy_install ropemacs
5. Install pyflakes for auto syntax check
sudo apt-get install pyflakes
6. Put everything together
Create init_python.el in your .emacs.d. Add following content
(autoload 'python-mode "python-mode" "Python Mode." t)
(add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode))
(add-to-list 'interpreter-mode-alist '("python" . python-mode))
(require 'python-mode)
(add-hook 'python-mode-hook
(lambda ()
(set-variable 'py-indent-offset 4)
;(set-variable 'py-smart-indentation nil)
(set-variable 'indent-tabs-mode nil)
(define-key py-mode-map (kbd "RET") 'newline-and-indent)
;(define-key py-mode-map [tab] 'yas/expand)
;(setq yas/after-exit-snippet-hook 'indent-according-to-mode)
(smart-operator-mode-on)
))
;; pymacs
(autoload 'pymacs-apply "pymacs")
(autoload 'pymacs-call "pymacs")
(autoload 'pymacs-eval "pymacs" nil t)
(autoload 'pymacs-exec "pymacs" nil t)
(autoload 'pymacs-load "pymacs" nil t)
;;(eval-after-load "pymacs"
;; '(add-to-list 'pymacs-load-path YOUR-PYMACS-DIRECTORY"))
(pymacs-load "ropemacs" "rope-")
(setq ropemacs-enable-autoimport t)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Auto-completion
;;; Integrates:
;;; 1) Rope
;;; 2) Yasnippet
;;; all with AutoComplete.el
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun prefix-list-elements (list prefix)
(let (value)
(nreverse
(dolist (element list value)
(setq value (cons (format "%s%s" prefix element) value))))))
(defvar ac-source-rope
'((candidates
. (lambda ()
(prefix-list-elements (rope-completions) ac-target))))
"Source for Rope")
(defun ac-python-find ()
"Python `ac-find-function'."
(require 'thingatpt)
(let ((symbol (car-safe (bounds-of-thing-at-point 'symbol))))
(if (null symbol)
(if (string= "." (buffer-substring (- (point) 1) (point)))
(point)
nil)
symbol)))
(defun ac-python-candidate ()
"Python `ac-candidates-function'"
(let (candidates)
(dolist (source ac-sources)
(if (symbolp source)
(setq source (symbol-value source)))
(let* ((ac-limit (or (cdr-safe (assq 'limit source)) ac-limit))
(requires (cdr-safe (assq 'requires source)))
cand)
(if (or (null requires)
(>= (length ac-target) requires))
(setq cand
(delq nil
(mapcar (lambda (candidate)
(propertize candidate 'source source))
(funcall (cdr (assq 'candidates source)))))))
(if (and (> ac-limit 1)
(> (length cand) ac-limit))
(setcdr (nthcdr (1- ac-limit) cand) nil))
(setq candidates (append candidates cand))))
(delete-dups candidates)))
(add-hook 'python-mode-hook
(lambda ()
(auto-complete-mode 1)
(set (make-local-variable 'ac-sources)
(append ac-sources '(ac-source-rope) '(ac-source-yasnippet)))
(set (make-local-variable 'ac-find-function) 'ac-python-find)
(set (make-local-variable 'ac-candidate-function) 'ac-python-candidate)
(set (make-local-variable 'ac-auto-start) nil)))
;;Ryan's python specific tab completion
(defun ryan-python-tab ()
; Try the following:
; 1) Do a yasnippet expansion
; 2) Do a Rope code completion
; 3) Do an indent
(interactive)
(if (eql (ac-start) 0)
(indent-for-tab-command)))
(defadvice ac-start (before advice-turn-on-auto-start activate)
(set (make-local-variable 'ac-auto-start) t))
(defadvice ac-cleanup (after advice-turn-off-auto-start activate)
(set (make-local-variable 'ac-auto-start) nil))
(define-key py-mode-map "\t" 'ryan-python-tab)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; End Auto Completion
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Auto Syntax Error Hightlight
(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 "pyflakes" (list local-file))))
(add-to-list 'flymake-allowed-file-name-masks
'("\\.py\\'" flymake-pyflakes-init)))
(add-hook 'find-file-hook 'flymake-find-file-hook)
(provide 'init_python)
7.Add (load-library “init_python”) in your .emacs file.
How to use it?
==========================
Use Tab when you want
1. Expand the code. ex. tab after “class” would triger class expansion
2. Complete code.
3. Tab
C-c d Display doc string
C-c c Run file in python
C-h m Show more key binding