Emacs as a powerful Python IDE

May 9, 2008 at 3:27 pm | In Emacs, Enigma Curry, Python |

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
    and
    if __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:


13 Comments »

RSS feed for comments on this post. TrackBack URI

  1. Nice post! Iswitchb comes standard with Emacs (with version 22 at least). Are there any advantages of using ido-mode over it? Thanks!

    Comment by Sergey — May 10, 2008 #

  2. Hi Sergey,

    ido-mode is actually an evolutionary descendent of iswitchb. The main addition to ido-mode is that it works for unopened files as well as buffers.

    Icicles is also another cool one. I haven't grown religious about any of them yet, and still haven't chosen 'The one' for my environment yet.

    Thanks for dropping by!

    Comment by ryan — May 10, 2008 #

  3. [...] Here is a great post on how to do the same with Emacs. [...]

    Pingback by sontek ( John M. Anderson ) » Python with a modular IDE (Vim) — May 11, 2008 #

  4. I have also found http://www.emacswiki.org/cgi-bin/emacs/blank-mode.el very useful when trying to sort out mixed tab / space nonsense (in addition to the built in tabify/untabify)

    Comment by Andrew — May 11, 2008 #

  5. [...] are a couple of good resources for configuring Vim and Emacs for Python development. I know more key-bindings with Emacs than Vim, so my preference is for [...]

    Pingback by Nick Carroll » Blog Archive » Old School Editors for Python Development — May 12, 2008 #

  6. You mentioned sontek using your environment as a template. I would like to do the same and was wondering what resources there are out there to make this easier. I've been using emacs in school for a while but never really looked into customizing it until now.

    Comment by linuxunil — May 29, 2008 #

  7. linuxunil,

    The "dot-emacs" file in my tarball above is the starting point for where things are loaded. I've sectioned each mode of emacs into various files called ryan-XX where XX is the name of the mode that the file configures. In the dot-emacs file you can simply comment out the loading of each ryan-XX file. If you comment out all of them you are left with a pretty much standard/default emacs install. You can then uncomment each of them, one at a time, as well as tweak the internals of each one individually.

    But, you may be just better off not using my environment at all and just reading it and taking out good ideas here and there. I believe that is what sontek is actually doing.

    Thanks for dropping by!

    Comment by ryan — May 29, 2008 #

  8. Aquamacs - I am home…

    If you like Emacs, and are looking for version that plays well in OS-X land, it looks like Aquamacs is……

    Trackback by paradox1x — May 30, 2008 #

  9. I've been using Emacs for a long time, not all 32 years, but a significant fraction, on many different computers. I've been using Python for 4-5 years, always with Emacs as my IDE. The one thing that drives me nuts is how the interpreter interacts with execute buffer/region/def/class. The buffer ends up in Python without its package, as if it had been typed into the interpreter. So the code behaves differently than if you were running from the command line. This is most troublesome if you've split your code up into multiple files. Any one have a fix for this?

    Comment by Gregg — June 13, 2008 #

  10. Great post, thanks! I tried using your setup, with Emacs 21.4.1 on Ubuntu 8.04 but I keep getting this error when starting up emacs:

    "Required feature erc was not provided"

    I tried commenting this out in the .emacs but then it complained that 'url' was not provided.

    Any suggestions?

    Comment by Justin — July 10, 2008 #

  11. Hi Justin,

    I may be mistaken, but I believe that ERC was not distributed with Emacs 21, so thats probably your problem.

    Like you tried, you should just be able to uncomment the erc bits. Be aware that in my dot-emacs there is 1) a require line near the top, 2) a ryan-erc line a bit futher down, and 3) several erc-* lines at the very bottom in the customize section.

    I'm currently using Emacs 23, which is the latest "emacs-snapshot" package on Ubuntu. It is still a development package, but it is rock solid stable in my tests. Emacs 22 is the current version you get when you install "emacs" on ubuntu 8.04. Both 23 and 22 include ERC.

    Emacs 21 is pretty old by now, is there a specific reason you're still using it?

    Comment by ryan — July 10, 2008 #

  12. Hey Ryan,

    Not sure why I had 21. I just installed the snapshot, and everything works!

    Thanks

    Comment by Justin — July 11, 2008 #

  13. same

    error: Pymacs got and invalid initial reply

    any suggestion

    Comment by Suren — September 13, 2008 #

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by WordPress.
Entries and comments feeds. Valid XHTML and CSS. ^Top^
XML Sitemap