Emacs as a powerful Python IDE
May 09, 2008 at 03:27 PM | categories: python, enigma curry, emacs | View CommentsUpdate 01/2009: this post is still valid, but see updated installation instructions here.
Last night at the Python user group I gave a short demo on using Emacs as a Python editor/IDE. My macbook pro refused to display on the projector so I thought my demo was going to be a 'no go'. Thankfully, sontek allowed me to use his Linux laptop. I hurriedly copied over my emacs environment, installed a few packages and was able to present after all. I think the demo went fairly well even though I think it was a bit hurried and I forgot to cover a few things, I think I was pretty nervous at the same time because of the fact that the mac didn't work and got me flustered. Oh well, I think people enjoyed it.
My Emacs Environment
Below are the Emacs features most applicable to Python development:
- Rope and Ropemacs
Rope is a general (non-emacs specific) Python IDE library. It has awesome support for multiple refactoring methods and code introspection. Inside Emacs, this gives us:
- Full (working!!) code-completion support of modules, classes, methods etc. (M-/ and M-?)
- Instant documentation for element under the cursor (C-c d)
- Jump to module/class/method definition of element under the cursor (C-c g). This works for any Python code it finds in your PYTHONPATH, including things from the stdlib.
- Refactoring of code (like rename -- C-c r r)
- List all occurences of of a name in your entire project
- and More.
- YASnippet
YASnippet is a snippet tool like TextMate. You can expand user defined keywords into whole blocks of predefined code. This is especially useful for the usual boilerplate that would go into a python file like
#!/usr/bin/env python
andif __name__ == '__main__':
Granted, Python doesn't require much boilerplate, and therefore this package is much more suited to languages like Java, but I bring it up because I think its cool and if you get into the habit of using it, then a few keystrokes saved here and there will add up over time.
- Subversion support with psvn.el Psvn is a comprehensive subversion client for Emacs. It integrates well with ediff mode so you can use it to check changes between versions. It does all of the other boring subversion stuff well too.
- Ido-mode for buffer switching and file opening. Emacs, to the uninitiated, can be confusing because by default there is only one view into a single file at a time. How does one get to another file? Instead of cluttering the interface with GUIness and making the user click somewhere (and thereby forcing the user to waste their time by moving their hand off of the keyboard), Emacs gives powerful ways to switch between files. Ido-mode is one of these useful ways -- it makes a list of open files starting with the most frequently visted files and widdles this list down as you type part of the filename. You can have dozens of files open and only be a few keystrokes away from any one of them.
A lot of people, for whatever reason, don't feel that Emacs is an IDE at all. I don't personally care what you define it as -- the fact remains -- Emacs is a powerful Python environment and despite being over 32 years old has proven to be just as modern as any IDE today, and remains THE most configurable editor (operating system?) ever.
I've tarred up my Emacs environment for general consumption. Instructions:
- Install Pymacs
- Install Rope and Ropemacs
- BTW, those three packages should be the only packages other than Emacs you'll need. Everything else is self contained.
- Extract the tarball to your home directory. This creates a directory called ryan-emacs-env.
- Rename "ryan-emacs-env" to ".emacs.d"
- Symlink my dot-emacs file to your .emacs. Run "ln -s .emacs.d/dot-emacs .emacs".
- If you also want to do Java development run "tar xfvz jde-2.3.5.1.tar.gz". I leave it tarred because you don't need to pollute your environment if you're not going to use Java. (Also for whatever reason, jde doesn't like to be stuck in my subversion repository so I just leave it tarred up and untar on every machine I check it out on.)
Extra tips:
- Put your .emacs.d directory under version control. Never rely on your distros emacs packages, install all future elisp files yourself in your .emacs.d file and commit to your repository often. This way you've got an environment that is easily transportable and synchronizable across multiple machines. This is the major reason why my emacs environment was so fast to trasnfer from my macbook pro to sontek's laptop during the demo.
- Speaking of sontek, he brought up an excellent point in #utahpython the other day, he's not going to be using my emacs environment except for reference, instead he's starting with a clean slate. This is by far the best and most prudent thing to do. My emacs environment is a culmination of several years of plugging in and deleting various packages and writing various snippets of elisp. Your needs are always going to be different than mine and you are also going to be better off by educating yourself along the way by creating your own.
Some more fun Emacs evangelism:
- a fascinating screencast of Emacs in the role of Ruby editor (many tips here apply to Python too)
- Emacs blogs I read:
- Emacs life
- Life is too short for bad code
- Minor Emacs Wizardry
- M-x all-things-emacs
- Platypope.org
- Know of any other good ones? Let me know.