Setting up Emacs for Python development
- Ubuntu packages to install:
emacs autocutsel texinfo git mercurial(git and texinfo are required by el-get; mercurial is required to install pymacs)
- To set the font size for just this session: press
M-:and then type
(set-face-attribute 'default nil :height 100)(taken from stack overflow)
(If you're using an old version of Ubuntu then you might need to use this PPA to get Emacs 24.3: Get the latest version of Emacs from this Ubuntu PPA)
- Basic .emacs from Imperial
A good, recent guide to installing Python tools in Emacs. Some tweaks:
- To use flycheck with Python:
sudo pip install --upgrade logilab-common pylint. Then disable warnings and conventions as per this SO post.
- you can also install pymacs with
- To use flycheck with Python:
Plug ins I use:
(require 'package) (add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/") t) (add-to-list 'package-archives '("marmalade" . "http://marmalade-repo.org/packages/") t) (when (< emacs-major-version 24) ;; For important compatibility libraries like cl-lib (add-to-list 'package-archives '("gnu" . "http://elpa.gnu.org/packages/"))) (package-initialize)
This allows you to easily install all these other plug ins:
- magit (better git support)
- buffer move (easily move buffers around)
- FlyCheck - "flymake done right"; just do
flycheck. Then do the 'verify setup' trick mentioned in the manual. I use
- ropemacs - refactoring, go to definition etc for python.
Other plugins which come with Emacs but require some configuration:
- recently used files
- 80 column rule
- setup emacs python mode to use ipython
- emacs-jedi (autocomplete, go-to-source etc)
- matlab-emacs - trying to install this with
el-getfailed. So I checked out
.emacs.dand added the two required lines to my
- see this answer about setting the shell colours
M-qreformat comment test. From SO.
- emacs-jedi keybinds
- EIN keybinds (also see the screenshots for more examples)
C-c C-ubrowse URL
- If you modify
.emacs, you don't need to restart Emacs for the changes to take effect. See this SO post.
- reload changes recursively in IPython
- magit cheat sheet
- debugging Python in Emacs
C-<number>-x tabrigidly indent region by
C-x C-v RETreload file
import ipdb; ipdb.set_trace()
M-%incremental search and replace
M-=show word and character count
C-c ggo to definition (using Rope)
Make errors into clickable links in iPython
sudo gunzip python.el.gz
sudo gedit python.el
line-start (1+ (any " \t"))from line 1609
sudo emacs -batch -f batch-byte-compile *.el
Config .emacs for ipdb.
Code is adapted from PedroKroger.net
;;---------------------------------------------------------------------------- ;; ipdb ;; from: pedrokroger.net/2010/07/configuring-emacs-as-a-python-ide-2/ ;; Highlight ipdb lines: (defun annotate-pdb () (interactive) (highlight-lines-matching-regexp "import pdb") (highlight-lines-matching-regexp "pdb.set_trace()")) (add-hook 'python-mode-hook 'annotate-pdb) ;;---------- ;; Keybinding to add breakpoint: (defun python-add-breakpoint () (interactive) (newline-and-indent) (insert "import ipdb; ipdb.set_trace()") (highlight-lines-matching-regexp "^[ ]*import ipdb; ipdb.set_trace()")) (define-key python-mode-map (kbd "C-c C-t") 'python-add-breakpoint)
Notes for using Emacs with C++
- FlyCheck: To get
clangto work correctly on Ubuntu 13.10 for checking C++ I had to install clang-3.4 using these sources because 13.04 currently ships with clang 3.2 which gives the error:
/usr/include/stdio.h:33:11: fatal error: 'stddef.h' file not found. The bug has been reported on launchpad.
- Two candidates for autocompletion appear to be AutoComplete and company-mode.
Some plug-ins I have used in the past but no longer use:
- flymake config for Python (i.e. enable on-the-fly checking of Python code) - I now use flycheck instead.
- Many (if not all) of the extensions below could be installed using Emacs24's package manager
- color themes
- iSwitch buffers (switch buffers by typing any part of the buffer name)
- Browse URL. I set
(setq browse-url-browser-function 'browse-url-firefox)and
(global-set-key "\C-c\C-u" 'browse-url-at-point)
- Emacs IPython Notebook (EIN)
- Collection of Emacs Development Environment Tools (CEDET). SpeedBar is especially cool. (CEDET is installed by default with ubuntu's emacs24 package, but you need to manually install CEDET to get the latest versions)
- spyder (scientific dev environment)
Update 2nd July 2012
I've gone back to using PyDev (an Eclipse plug-in) for my Python development. I do enjoy Emacs but PyDev is simply too pleasurable and powerful to ignore. Plus PyDev appears to be receiving increasing amounts of development effort, in contrast to some of the Emacs tools I experimented with. And Eclipse offers enough configuration options to mean that I can get Eclipse to feel pretty "Emacs-like". The notes below are only about two thirds complete but they might still be useful...
So, I've settled on using Python 2.7 for the development of my disaggregation system. I'm very happy with Python so far. The more I learn about Python, the more excited I get; which is the oposite to the situation I experienced with MATLAB: the more I learnt about MATLAB, the more repulsed I became.
So, the next question becomes: which IDE to develop in? I spent a few days using the Eclipse plugin PyDev but several things started to bug me; notably the fact that the interactive console doesn't respect the correct Eclipse keybindings. Plus Eclipse requires lots of messing about with the mouse which can slow you down a bit. So I started thinking seriously about using my old friend Emacs to develop Python in. Pedro produced an excellent blog post describing how to install a bunch of add-ons to Emacs to make it a powerful Python IDE. Unfortunately things have changed a bit since 2010 when that blog post was produced. Hence this blog post is a list of modifications to Pedro's blog post to bring it up to date. I certainly don't intend to replicate Pedro's excellent work; I'm just producing a "diff" to bring his blog post up to date. There's also some good notes on vanguard33's blog.
- Install iPython
- Install python-mode into
~/.emacs.d/as per the instructions in the
- Don't bother manually putting
(setq py-shell-name "ipython")into your
~/.emacsfile. Instead, in emacs, type
M-x customize-variable py-shell-nameand change it to
easy_install rope ropemacs. Modify
.emacsto get ropemacs to work as described in the ropemacs readme. Install Pymacs (there's a bug in the install at the time of writing; to get the install to work follow the step described in the bug report fix install of Pymacs on Python 2.7 (issue #28)). You might also need to add
(setq py-load-pymacs-p 'nil)to your
- Magit is now hosted on github.
Tools I tinkered with but am not using
emacs-for-python looks like a quick and easy way to install lots of useful stuff. But I decided to install individual packages.
ipython.el: I'm not certain if
ipython.el is required or not (the latest version can be found
in the ipython repository on
There's lots of discussion about
and github. There's also
some discussion in the ipython editors.txt
(which I need to read). this recent comment by Andreas
ipython.el should no longer be
necessary. My assumption for the time being is that
ipython.el is not required.
The anything project has been
superceded by the helm project.
You can get the files using something like
cd ~/.emacs.d; git clone git://github.com/emacs-helm/helm.git and then read the
README.md file. There is also
a helm-ipython project.
HOWEVER, helm-python requires ipython.el (I submitted a bug
report); and I'm
fairly sure that ipython.el is now redundant.
auto-complete works pretty
well. I grabbed the latest auto-complete from
github. Before installing, it's
necessary to also grab popul-el and
fuzzy-el and put these two files in
auto-complete directory prior to
installing. (It looks like the author has split fuzzy-el and popup-el
from auto-complete but hasn't updated the documentation yet.) I like
autocomplete to only offer help when I explicitly ask for help so I
(ac-set-trigger-key "TAB") (setq ac-auto-start nil) to my