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.
Follow these instructions first to customize Emacs for editing Python code.
To start the Python interpreter, type python at the Linux prompt.
You can load in definitions from a module in one of two ways. Suppose you have a file called whatever.py that defines various functions and classes, including the function main(). One way to load and run main is shown below:
>>> import whatever >>> whatever.main()
You have to include the prefix "whatever." when referring to functions and classes in the module. This approach has the advantage that if you make changes to the definitions in whatever.py, you can reload the module interactively by typing:
>>> reload(whatever)
Alternatively, you can load in whatever.py this way:
>>> from whatever import * >>> main()
If you use the from form of import, you don't need to include the module name as a prefix when referring to functions and classes. However, you won't be able to use reload to re-read your definitions after editing them. Instead, you'll need to start a new Python session.
Typing python -i whatever.py at the Linux prompt loads in your definitions on startup as if you had typed from whatever import * at the Python prompt.
To exit the Python interpreter, type CTRL-d at the Python prompt.
To start Emacs, type emacs at the Linux prompt.
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:
In the shell interaction window, CTRL-c CTRL-e repositions the cursor at the Python prompt.
To re-execute an earlier Python command in the interaction window, just place the cursor on the earlier line (or use CTRL-r to search backwards for a particular string) and then hit Return. You can also edit the earlier line if desired before hitting Return.
To reset the interpreter (or interrupt an infinite loop, etc.) type CTRL-c CTRL-c.
To uniformly shift a region of Python code one indentation level to the right, highlight the region with the mouse and type CTRL-c > (or CTRL-c < to go left).
To comment out a region of Python code, highlight the region with the mouse and type CTRL-c c (or CTRL-c u to uncomment it).
To move the cursor from its current position to the beginning of the innermost surrounding block of code type CTRL-c CTRL-u.
To move the cursor from its current position to the beginning of the current function or method definition type CTRL-ALT-a.
To move the cursor from its current position to the end of the current function or method definition type CTRL-ALT-e.