Note This is the original post from 2009 – both Emacs and Python have evolved since then. By the time this note has been added in 2023 the text is very much out of date!
Tips on running python from Emacs.
Python interpreter to use
If you have more than one version of python installed on your system
you may want to tell emacs which one of those to use. The python
interpreter that emacs will use is controlled by the
py-python-command variable. You can set it with:
(setq py-python-command "/usr/local/bin/python2.3")
Associating files with python
Emacs will normally associate files ending with
.py with python
and enter python-mode if you open (visit) these files. You can
associate other files with python through the use of the
For example, scons is a popular build environment tool built on top of
python. Its configuration files are in python themselves, so it is
useful to be able to edit them in python-mode. This can be arranged by
placing the following into your
(setq auto-mode-alist (cons '("SConstruct" . python-mode) auto-mode-alist))
You can adjust the shell environment under which the python
interpreter will run, including the content of the
variable, using commands described in this entry.
All of these work only in Python-mode, i.e., if you’ve open a file with a suffix .py.
C-c !to start a python interpreter as a sub-process, or if already started, to switch to this buffer.
C-c C-cto send the entire current file to the interpreter
C-M-xto send the current function definition the interpreter
C-c retto reload current file as python module