Using Python with Emacs

Back

Introduction

In this page, I will explain my emacs environment for programing and interactive session with Python. I put emphasis on my because this may not be the most elegant solution (and probably it isn't). The effectiveness of a human interface depends a lot on the user's preference. Therefore, the example shown here strongly reflects my personal preferences. You may want to change details of the behavior. You can do so but you have to know a bit about Emacs Lisp. Since I don't have a time to explain Emacs Lisp here, I will just refer you to two nice documents for learning Emacs Lisp, An Introduction to Programming in Emacs Lisp and GNU Emacs Lisp Reference Manual.

My computer environment

Before going into the details of the emacs setting, I will list my computer environment as a reference.

HardwarePC (Sony VAIO type Z)
OSUbuntu 9.10 (x86-64)

Additional packages

In addition to Emacs, you need to install several packages. I took most of the information from here.

.emacs file

Once you have installed the above packages, edit your .emacs file.
Here is an example of .emacs file. You can merge this file into your .emacs file. Please read the comments in the file to see what is going on.

Usage

Notation

In this tutorial, key bindings are explained often. The notation for key strokes are in the following form.

Example1:
[C-c x]

The square brackets [] indicate that this is a suit of key strokes. C-c means press c while holding Ctrl down. Therefore the meaning of the above example is to hit c with Ctrl pressed then hit x *without* Ctrl.

Example2:
[C-c C-x]

In this case, you have to keep Ctrl pressed when you hit x.

Example3:
[M-x TAB]

M indicates a meta key. On a PC, it is usually bound to Alt key. TAB is of courese the TAB key.

Example4:
[M-C-x]

Here you are required to press Meta and Ctrl and x at the same time.

Example5:
"[M-x] emacs-function"

[M-x] is used to invoke an emacs function. After pressing [M-x], you enter the name of the emacs function in the mini buffer. TAB completion is available.

Starting up

When you invoke Emacs, supply "-py" option. This is necessary because in my .emacs, this option is used to determine whether to load pymacs and ropemacs or not. pymacs and ropemacs are not loaded by default because it takes some time and delays the start up of Emacs. You do not want to load them unless you know you are going to use rope. If "-py" option is used, Emacs will load pymacs and ropemacs at the start up. If you did not specify "-py" but want to use rope later in the session, you can call my-pymacs-init (by typing "[M-x] my-pymacs-init") at any time.

Python-mode

If you open a file with an extension ".py", the python-mode will be automatically invoked. You will have nice syntax highlighting and automatic indentation. In addition, you can do the followings (and much more actually):

Ropemacs

If you installed ropemacs, you have access to the following functionalities (and much more !):

Since rope is a refactoring library, you can perform many refactoring operations. You can access those functionalities from the rope menu.

Folding mode and cells

I use folding-mode to realize cell functionality in emacs. A cell is defined as a region enclosed with #{{{ and #}}}. You can open and close cells using right-mouse-click or keyboard shortcuts explained below. You can also execute a cell by [C-c C-j]. Cells can be nested. You can specify the title of a cell after the opening parenthesis like this:

#{{{ Cell title

#}}}

By using cells, you can improve the readability of your code. Also splitting scripts into small chunks of codes is useful for cut-and-try style work flow of scientific computing.

Warning: Currently, folding-mode and ropemacs are not fully compatible. The rope functions often fail if the cells are closed. Hit [C-c C-f C-o] to open all the cells in the buffer, before using rope commands.

Flymake and pylint

Flymake is a minor-mode to compile the current code on the fly in the background and report any compilation error on the buffer. In python, there is no compilation process, but you can check the syntax errors using various lint programs. My .emacs is configured to use flymake with pylint.

The flymake mode is turned off by default. You can turn it on from the menubar [python] -> [Toggle flymake mode]. You can also toggle it by "[M-x] flymake-mode".

Parentheses check

To show the correspondence of parentheses, turn on the show-paren-mode by "[M-x] show-paren-mode".

"[M-x] blink-matching-open" may be also useful.

Window management

An emacs frame (window in a usual sense) can be divided into several windows (frames in a usual sense). Those windows can be resized using mouse. But you may want to use keyboard shortcuts.

Additionally I added the following key bindings in my .emacs

(global-set-key "\C-x7" 'shrink-window)
(global-set-key "\C-xl" 'previous-multiframe-window)

Debugger

You can invoke python debugger (pdb) by "[M-x] pdb". You are asked to enter the arguments for pdb. Usually you just give the file name of your script.

After running pdb, you will see a pdb prompt and the source of the file you are debugging in separate windows.

You can set a break point in a file by going to the source file buffer and pressing [C-x SPACE].


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS