Emacspython-modeThere 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.elThe 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.elThe 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 modesIn both cases opening python source files should automatically activate the python-mode:
(add-to-list 'auto-mode-alist '("\\.py$" . python-mode))
PDBThe 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 modeThe 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)
ELSEA 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. LinksPython debuggingYou can get more information on my python debugging module here. Adding history and command line completionThere 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 filesThis needs to be added to ~/.bashrc:
export PYTHONSTARTUP=${HOME}/.pystartup
Adding the ~/.pystartup fileAnd 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
LinksPython links
Back to Personal Wiki |