Using Emacs with Python

This page provides some instructions for using Emacs with Python. If you're not a regular Emacs user, I would strongly recommend taking some time to learn the basics now, because this will save you an enormous amount of time and effort (and probably frustration) in the long run. Emacs has a built-in tutorial that can be started by typing the command CTRL-h t.

Running Python and Emacs on the Pomona Linux machines


Editing in Python mode

In general, it's more useful to run an interactive Python session inside Emacs, rather than in a terminal window. This allows you to use all of the powerful editing features of Emacs in your Python session. To run Python within Emacs, first start Emacs and then type ESC x shell followed by a Return. This will give you a shell prompt. Then just type python as usual to start the interpreter.

You can then split the Emacs window into two or more subwindows using CTRL-x 2 (for horizontal subwindows) or CTRL-x 3 (for vertical subwindows), and open your Python file in one of the subwindows. That way, you can see and edit your definitions alongside your interactive Python session. To toggle between the subwindows, type CTRL-x o. To open a file in a subwindow, type CTRL-x CTRL-f. See the CS 151 Emacs Quick Reference Guide for more information about basic editing commands.

You should use the extension .py for your Python source files. This will cause Emacs to automatically enter Python mode when a Python file is opened.

Here are some useful commands to know about in Python mode: