Welcome to McFunley.com Sign in | Join | Faq

Setting up a Python Development Environment for the Ignorant Windows Programmer

posted on Saturday, December 02, 2006 5:18 PM by mcfunley

Update: someone informs me that there's an eclipse plugin, which seems obvious in retrospect. I stand by this since I think emacs is a better editor, but, for those who don't know emacs that surely makes more sense.
----
Another update: see here.
----

I have recently been doing some coding in Python. (I am gradually becoming obsessed with it, actually.) Perhaps the thing I like most about it is the fact that it has such a good interop story.

Getting myself to the point where I felt like I had an efficient development environment took a little while, though. I am not thrilled with IDLE (the python GUI that ships with the distribution), and I am trying to write portable code here so IronPython/VS.NET is not really an option.

So, here are the steps I followed to set up what is (in my opinion) a kickass alternative development environment for Python on Windows. This assumes very little knowledge and hopefully this will be helpful to somebody.

You will need:
Yes, tragically, you will have to learn emacs if you have managed to resist it up to this point. Luckily, it turns out to be worth the trouble.

Step 1 - Organizing

You can put these things wherever you want, but you must remember to make sure that python.exe is somewhere in your PATH. While you are at it you also should set a HOME environment variable so that emacs knows where to look for its startup file (.emacs).

I extracted the python-mode files to /lisp/progmodes/python-mode under my emacs folder.

Step 2 - Editing .emacs

Create a file called .emacs in the folder you chose as HOME in step 1 (there are actually a few different ways you can set this up, see here). Add the following to that file:
(add-to-list 'load-path "[the path to the python-mode directory]")
(setq auto-mode-alist (cons '("\\.py$" . python-mode) auto-mode-alist))
(setq interpreter-mode-alist (cons '("python" . python-mode)
                                      interpreter-mode-alist))
(autoload 'python-mode "python-mode" "Python editing mode." t)
(global-font-lock-mode t)
(setq font-lock-maximum-decoration t)

After doing that restart emacs.

Step 3 - Write Some Code

At this point you should have a working environment. When editing a .py file in emacs, you should see two related menu items--look there for the keyboard shortcuts or read the comments in the python-mode source. The coolest one is C-c ! (that's ctrl+c, then !, for the uninitiated), which opens up the Python REPL in a buffer.

You should now have something looking like this:



Although, your colors won't be as cool as mine unless you follow:

Step 4 - Optional Stuff

The default emacs colors on Windows remind me of a tour of a plastic vomit factory I had back in the early 90s, so one of the first things I did when I started using it (for Lisp, a few months ago) was figure out how to change them. Download color-theme and add this to your .emacs:
(add-to-list 'load-path "[the path to the color-theme folder goes here]")
(require 'color-theme)
(color-theme-initialize)
(color-theme-blue-mood) ; replace with whatever theme you want

I also switched the default font to Consolas, which I have become quite fond of. Here is how you do that:
(set-default-font "-*-Consolas-normal-r-*-*-13-*-*-*-c-*-*-iso8859-1")
Which seems awfully arcane to me, but there you go. Good night and good luck.

Comments

# re: Setting up a Python Development Environment for the Ignorant Windows Programmer @ Sunday, December 03, 2006 5:19 AM

...or just use the Eclipse plugin.

  TrueTom

# re: Setting up a Python Development Environment for the Ignorant Windows Programmer @ Sunday, December 03, 2006 6:33 AM

Heh, that's for cowards.

  Dan McKinley

# re: Setting up a Python Development Environment for the Ignorant Windows Programmer @ Sunday, December 03, 2006 8:18 AM

Consider making those
#comments
#about defs
def spam():
"""Into docstrings, which show up as spam.__doc__ and from which pydoc can generate documentation. See also, ipython. """
pass

  Jeremy Dunck

# re: Setting up a Python Development Environment for the Ignorant Windows Programmer @ Sunday, December 03, 2006 4:50 PM

Yeah, good idea, thanks

  Dan McKinley

# Why Emacs? @ Sunday, December 03, 2006 7:18 PM

Only a small fraction of Windows Python programmers use emacs. I am a bit distressed that your article implies that Emacs is a pre-requisite for Python on Windows. Nothing could be further from the truth! "Yes, tragically, you will have to learn emacs if you have managed to resist it up to this point." What about PythonWin, Komodo, Wing IDE, and all of the other Windows IDEs and editors that are listed when you Google "Python IDE".

  Paul Prescod

# re: Setting up a Python Development Environment for the Ignorant Windows Programmer @ Monday, December 04, 2006 5:42 AM

Response here:
http://mcfunley.com/cs/admin/blogs/posteditor.aspx?App=dan&PostID=1259

  Dan McKinley

# re: Setting up a Python Development Environment for the Ignorant Windows Programmer @ Monday, December 04, 2006 6:24 AM

You should definetely try Ipython (http://ipython.scipy.org/moin/) - they have Emacs mode for python-mode as well: http://ipython.scipy.org/dist/ipython.el

It's way more powerful than the standard interpreter.

  Victor

# re: Setting up a Python Development Environment for the Ignorant Windows Programmer @ Tuesday, December 05, 2006 9:31 AM

I really prefer the e text editor (TextMate port for Windows):

http://www.e-texteditor.com

When you first have tried the smart snippets, you can't go back :-)

  Joren Bonner

SKIN NAME : ImageHeader