Home Search Download Documentation
Help News Community SIGs
Python Mode
Introduction
python-mode.el (~123KB)
Installation Notes
What's Changed
FAQ
Other Emacs Goodies on Python.org
 
Email Us
python-mode@python.org
 
  

Installation Notes

If you are using XEmacs 21.4.4, you already get python-mode.el 4.1 for free, if you've installed the prog-modes package. Follow the instructions below if you want to install the latest python-mode.el version on XEmacs, or if you want to install it in any version of Emacs. Note that python-mode.el version 4.6 is not compatible with any XEmacsen earlier than 21.1 or Emacsen earlier than 20.7.

Byte Compiling

You will want to byte-compile python-mode.el, primarily for better performance. Do this by running the following commands inside X/Emacs:
C-x C-f /path/to/python-mode.el RET
M-x byte-compile-file RET
You can safely ignore any byte compiler warnings; they are due to the cross X/Emacsen support.

Be sure to see the list of compatibility issues, for special notes about X/Emacs versions and package interactions.

Setting load-path

You need to make sure that this new version of python-mode.el is on your load-path, before any version that is distributed with your X/Emacs. You can use this test:
M-x locate-library RET python-mode RET
Make sure this finds the one you expect it to find. If not, you can add this to your .emacs file:
(setq load-path (cons "/dir/of/python-mode/" load-path))

File Associations

If you are using Emacs, you will want to associate .py files with python-mode so that Emacs sets the major mode properly when you visit such files. This is controlled by two variables, auto-mode-alist and interpreter-mode-alist. Add the following to your .emacs file:
(setq auto-mode-alist
      (cons '("\\.py$" . python-mode) auto-mode-alist))
(setq interpreter-mode-alist
      (cons '("python" . python-mode)
            interpreter-mode-alist))
You don't need to do this for XEmacs, since as of at least XEmacs 19.15, these file associations are set by default.

Autoloading

The final piece of the puzzle is to associate python-mode with the file python-mode.elc. You do this by setting up an autoload by adding this to your .emacs file:
(autoload 'python-mode "python-mode" "Python editing mode." t)
Again, you only need to do this with Emacs, since as of at least XEmacs 19.15, this autoload is set up by default.

Syntax Highlighting

If you are using XEmacs, you should get syntax highlighting (a.k.a. font-locking turned on automatically. In Emacs, you might need to enable it directly. These lines should help:
;;; add these lines if you like color-based syntax highlighting
(global-font-lock-mode t)
(setq font-lock-maximum-decoration t)