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
el-get-install pymacs
andel-get-install ropemacs
- To use flycheck with Python:
Plug ins I use:
First configure package.el
to use
MELPA and
marmalade. This is what I put in my
.emacs
file:
This allows you to easily install all these other plug ins:
- highlight-symbol
zenburn-theme
- magit (better git support)
- buffer move (easily move buffers around)
- FlySpell
- FlyCheck - “flymake done
right”; just do
package-install
flycheck
. Then do the ‘verify setup’ trick mentioned in the manual. I useflake8
. - goto-last-change
- 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-get
failed. So I checked outmatlab-emacs
into.emacs.d
and added the two required lines to my.emacs
Some commands
- see this answer about setting the shell colours
M-q
reformat comment test. From SO.- emacs-jedi keybinds
- EIN keybinds (also see the screenshots for more examples)
C-z
undo (orC-_
)C-g C-_
orC-g C-z
redoC-c C-u
browse 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 tab
rigidly indent region byspaces. e.g. `C-4-x tab` or `C-minus-4-x tab` C-x C-v RET
reload fileC-c C-t
insertimport ipdb; ipdb.set_trace()
M-%
incremental search and replaceM-=
show word and character countC-c g
go to definition (using Rope)
Fix ModuleNotFound error
Create ~/.emacs.d/ipython_startup.py
:
def set_path():
import sys
old_path = sys.path
sys.path = [path for path in sys.path if path]
if len(sys.path) < len(old_path):
print("Removed '' from path!")
set_path()
Change the elpy section of .emacs
to look like:
(elpy-enable)
(setq
python-shell-interpreter "ipython"
python-shell-interpreter-args "/home/jack/.emacs.d/ipython_startup.py -i --simple-prompt")
Adapted from https://emacs.stackexchange.com/a/43978
Enable autoreload
See https://stackoverflow.com/a/43020072
Make errors into clickable links in iPython
cd /usr/share/emacs/24.3/lisp/progmodes
sudo gunzip python.el.gz
sudo gedit python.el
- Remove
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
Notes for using Emacs with C++
- FlyCheck: To get
clang
to 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
M-x list-packages
- 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)
Alternative editors
- 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 theREADME
andINSTALL
files - Don’t bother manually putting
(setq py-shell-name "ipython")
into your~/.emacs
file. Instead, in emacs, typeM-x customize-variable py-shell-name
and change it toipython
easy_install rope ropemacs
. Modify.emacs
to 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.emacs
- Magit is now hosted on github.
Useful resources
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
github).
There’s lots of discussion about ipython.el
on
StackOverflow
and github. There’s also
some discussion in the ipython editors.txt
file
(which I need to read). this recent comment by Andreas
Röhler
suggests that ipython.el
should no longer be necessary. My assumption
for the time being is that ipython.el
is not required.
Autocompletion
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
the 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 added
(ac-set-trigger-key "TAB") (setq ac-auto-start nil)
to my .emacs