auf.kante
Gunnar macht sich selbststaendig und fliegt auf
die Schnauze
wird erfolgreich. Wer mag darf zuschauen.
Gunnar is starting his business. He will certainly
fail succeed. You may watch.
auf.kante
Website sections
Home
Python
Last changed: 2006-02-05 [11:27]
Content:

Information about Python
 
Back to Personal Wiki
Table of contents
Emacs
Python debugging
Adding history and command line completion
Python links

Emacs

python-mode

There currently exist two python-modes for emacs. The first is the current standard mode for python but the development version of Emacs comes with a new python-mode. Both versions seem to be roughly equivalent in terms of functionality. Some features are still missing from the new version but symbol completion only worked with the new version for me.

Since I used both modes I will describe the setups for both of them. python-mode.el refers to the older version, python.el to the newer variant.

python-mode.el

The installation path for the package has to be added to the load path:

(setq load-path
      (cons "/usr/share/emacs/site-lisp/python-mode" load-path)

This sets the basic syntax parameters for the python mode:

(setq interpreter-mode-alist
      (cons '("python" . python-mode)
          interpreter-mode-alist)

      python-mode-hook
      '(lambda () (progn
           (set-variable 'py-indent-offset 4)
           (set-variable 'py-smart-indentation nil)
           (set-variable 'indent-tabs-mode nil) )))

And finally auto-loading should be activated to speed up the start up process:

(autoload 'python-mode
  "python-mode" "Python editing mode." t)

python.el

The basic settings of the module are ok but tabs are activated for indenting. This is something I turn off:

(setq python-mode-hook
      '(lambda ()
         "Turn off Indent Tabs mode."
         (set (make-local-variable 'indent-tabs-mode) nil)))

And auto-loading should be added:

(autoload 'python-mode
  "python" "Python editing mode." t)

There was one thing that was not working (actually a very essential thing). When sending a region or a buffer to the python process the code did not get evaluated in the global scope of the process.

Changing eexecfile in /usr/share/emacs/22.0.50/etc/emacs.py to

# The variable dictionaries can now be specified from the outside.
# This way the evaluation reaches the global scope in the interpreter.
def eexecfile (file, g, l):
    """Execute FILE and then remove it.
    If we get an exception, print a traceback with the top frame
    (oursleves) excluded."""
    try:
	try: execfile (file, g, l)
	except:
	    (type, value, tb) = sys.exc_info ()
	    # Lose the stack frame for this location.
	    tb = tb.tb_next
	    if tb is None:	# print_exception won't do it
		print "Traceback (most recent call last):"
	    traceback.print_exception (type, value, tb)
    finally:
	os.remove (file)

and adapting the function python-send-region in python.el:

         ;; specifying the globals of the interpreter as evaluation context
	 (command (format "emacs.eexecfile(%S,globals(),globals())" f))

allowed me to fix the problem.

But I guess I just misunderstood how the function is intended to be used. Or maybe something changed in python-2.4.

Both modes

In both cases opening python source files should automatically activate the python-mode:

(add-to-list 'auto-mode-alist '("\\.py$" . python-mode))

PDB

The following option allows to run the python debugger pdb within emacs (provided that the pdb script is located at /usr/lib/python2.4/pdb.py):

(setq pdb-path '/usr/lib/python2.4/pdb.py
      gud-pdb-command-name (symbol-name pdb-path))

Emacs would usually asks to provide the file name of the script you wish to debug without providing the current buffer as a default value. The following lines will correct that:

;; http://lists.gnu.org/archive/html/help-gnu-emacs/2003-10/msg00577.html
(defadvice pdb (before gud-query-cmdline activate)
  "Provide a better default command line when called interactively."
  (interactive
   (list (gud-query-cmdline pdb-path
			    (file-name-nondirectory buffer-file-name)))))

Doctest mode

The doctest mode is only provided by the older python-mode variant.

Even if the new python-mode is used, the doctest definition can be loaded from the older variant:

(setq load-path
      (cons "/usr/share/emacs/site-lisp/python-mode" load-path)

(add-to-list 'auto-mode-alist '("\\.doctest$" . doctest-mode))

(autoload 'doctest-mode
  "doctest-mode" "Editing mode for Python Doctest examples." t)

ELSE

A template system for emacs that helps with the code skeleton. I use my own ebuild to install it for emacs.

Once the code has been installed, the code needs to be imported into emacs.

;; load path for else-mode
(add-to-list 'load-path "/usr/share/emacs/site-lisp/else-mode")

;; my own modified templates for ELSE reside here
(add-to-list 'load-path "~/usr/devel/website/config/emacs/else-mode")

;; autoload on function 'else-mode'
(autoload 'else-mode "else-mode" "Starting template engine" t)

;; automatically start with python
(add-hook 'python-mode-hook
	  (lambda ()
	    ;; this is shown as a lambda so you can add further interesting
	    ;; minor mode definitions here.
	    (else-mode)))

The documentation of the package is pretty good, so just check out the info file in order to see what is possible with ELSE.

Links

Same section in del.icio.us
  • Debugging python in emacs
  • Emacs Language Sensitive Editor (ELSE)

Python debugging

You can get more information on my python debugging module here.

Adding history and command line completion

There exists a useful feature for the python interpreter: The PYTHONSTARTUP variable.

It can be set to a python source file that will be executed when python is started in an interactive mode. This can be used to add command line completion and a history.

Adding PYTHONSTARTUP to the bash startup files

This needs to be added to ~/.bashrc:

export PYTHONSTARTUP=${HOME}/.pystartup

Adding the ~/.pystartup file

And the following code will add history as well as the command line completion.

File ~/.pystartup*:

import atexit
import os

try:
    import readline
except ImportError:
    print "Module readline not available."
else:
    import rlcompleter
    readline.parse_and_bind("tab: complete")

historyPath = os.path.expanduser("~/.pyhistory")

def save_history(historyPath=historyPath):
    import readline
    readline.write_history_file(historyPath)

if os.path.exists(historyPath):
    readline.read_history_file(historyPath)

atexit.register(save_history)
del os
atexit
readline
rlcompleter
save_history
historyPath

Links

Same section in del.icio.us
  • pycomplete for Emacs python mode
  • rlcompleter - Command line completion in the Python Interpreter

Python links

Same section in del.icio.us
  • Delicious Python - use the delicious Api from Python
  • python/delicious
  • ActiveState O'Reilly Python cookbook code samples ratings review
  • How to Use UTF-8 with Python
  • Python and Unicode
  • Python Unicode Tutorial
  • M2Crypto - A Python crypto and SSL toolkit
  • SourceForge.net: Project Info - Media Metadata for Python
  • Python stuff for cyrus imapd
  • Python Unit Testing Framework
  • PyBlosxom - main site
  • Python API for IMDB
  • Webcheck - A website link checker
  • Python FUSE Bindings
  • Emacs Python Programming tips
  • Pymacs - Emacs Mode
  • Debugging python in emacs
  • Python
  • XML.com: Introducing del.icio.us
  • pycomplete for Emacs python mode
  • PyLint - Source Code Quality Checking for Python
  • rlcompleter - Command line completion in the Python Interpreter
  • PyChecker - Python Source Code checking
  • List of open webapp-config bugs
  • py-complete.el - Another completion tool for pymacs
Back to Personal Wiki
Creative Commons-Lizenzvertrag
The content of this site is licensed under a Creative Commons 2.5 License [attribution, share-alike]