Emacs is a great text-editor and highly extensible. I saw people on Youtube controlling their espresso machine with Emacs. So it should be possible to do some Python programming with this Swiss army-knife. The goals are syntax highlighting, code-completion, syntax-checking and code short-cuts. I'm using my beloved Fedora and Emacs 23.2. Usually, Emacs is already part of every Linux standard installation otherwise you'll find it in your package manager for sure!

I'm using the python-mode.el created by the python-community.

  1. download the python-mode.el file into your emacs.d folder:
    cd ~/.emacs.d
    curl http://launchpadlibrarian.net/21781107/python-mode.el
  2. add this lines to your .emacs file in your home directory to use the python-mode:
    (setq auto-mode-alist (cons '("\\.py$" . python-mode) auto-mode-alist))
    (setq interpreter-mode-alist (cons '("python" . python-mode)
    (autoload 'python-mode "python-mode" "Python editing mode." t)
    (global-font-lock-mode t)
    (setq font-lock-maximum-decoration t)
    (setq-default indent-tabs-mode nil)
    (setq default-tab-width 4)

Next we have to install some more software. Some of this software is written in Python (such a surprise!) and the probably easiest way to install this kind of software is by using python-pip. So we need to install python-pip first:

yum install python-pip

Now we can move on to install Ropemacs. This Python refactoring library will offer code completion among other things.

  1. installing the python part of Rope, Ropemacs and Pymacs
    python-pip install http://pymacs.progiciels-bpi.ca/archives/Pymacs.tar.gz
    python-pip install http://bitbucket.org/agr/ropemacs/get/tip.tar.gz
  2. installing the Emacs part:
    cd ~/.emacs.d
    curl http://pymacs.progiciels-bpi.ca/archives/Pymacs.tar.gz | tar zx
    cd Pymacs-0.23
    emacs -batch -eval '(byte-compile-file "~/.emacs.d/Pymacs-0.23/pymacs.el")'
  3. add this lines to your .emacs file to use Ropemacs:
    (add-to-list 'load-path "~/.emacs.d/Pymacs-0.23")
    (require 'pymacs)
    (pymacs-load "ropemacs" "rope-")
    (setq ropemacs-enable-autoimport t)

Next we are using auto-complete mode which offers nice code completion using drop-down menus which works perfectly together with Ropemacs:

  1. we need to download and compile the auto-complete first:
    cd ~/.emacs.d
    curl http://cx4a.org/pub/auto-complete/auto-complete-1.3.tar.bz2 | tar jx
    cd auto-complete-1.3
    make byte-compile
  2. next we need to edit the .emacs file again to use the auto-complete mode:
    (add-to-list 'load-path "~/.emacs.d/auto-complete-1.3")
    (require 'auto-complete-config)
    (add-to-list 'ac-dictionary-directories "~/.emacs.d/vendor/auto-complete-1.2/dict")

Next step is installing the Yasnippet (Yet another snippet) template system which offers nice code templates which helps you with writing code faster.

  1. we need to download the Yasnipplet bundle first: go to yasnippet and download the yasnippet-BUNDLE file into your ~/.emacs.d directory and unpack it
  2. add the following line to your .emacs file:
    (require 'yasnippet-bundle)

Last step is the on the fly code and syntax checker. We use flymake which already comes with Emacs and extend it by Pyflakes and pep8 which is a syntax style checker.

  1. python-pip install pyflakes pep8
  2. save the flymake-cursor.el by copy and pasting the code into a text file and save it as flymake-cursor.el into your ~/.emacs.d directory.
  3. add this lines to your .emacs file:
    (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
    (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)
  4. create a text file called pycheckers and save it with the following code somewhere into your load PATH
    pyflakes "$1"
    pep8 --ignore=E221,E701,E202 --repeat "$1"

That's it. Depending on how experienced you are the entire procedure takes you 20 minutes and at the end you get a perfect Emacs Python IDE!

Reference: www.saltycrane.com