12

I have been trying to configure Emacs to use the proper Python mode. To this end I have read a few tutorials. Most importantly this one. The tutorial uses the python-mode package. However, when I install it through the package manager (M-x package-install RET python-mode RET) I can not use the shortcuts that are available for that package.

I opened up a Python file to test some shortcuts such as C-c |, which should evaluate the selected expression. However, to my surprise these did not work so I decided to find out other tutorials on the topic. However, in my mode-line I do see Python, so there is some python-mode enabled.

On the emacs wiki I found the following snippet to add to my init.el file:

 (autoload 'python-mode "python-mode" "Python Mode." t)
 (add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode))
 (add-to-list 'interpreter-mode-alist '("python" . python-mode))

When I add this the python-mode actually works and the aforementioned shortcuts work as well.

I do not understand properly what this does more than manually executing M-x python-mode in a python file buffer.

When I removed every trace of python-mode in my .emacs folder and opened up a python file I noticed that I still have a python-mode. So my guess is that there are two python-modes?

I think I want the python-mode from https://launchpad.net/python-mode. It seems that it is present in the package repository, but I am unsure how to remove the other Python mode.

Could somebody elaborate please?

  • 1
    If you want to use Ipython, [ein](https://github.com/millejoh/emacs-ipython-notebook) is an excellent package. It gives you the equivalent of Ipython notebook in Emacs. [Demos here](https://github.com/millejoh/emacs-ipython-notebook/wiki/Screenshots).There are also [old demos](https://github.com/tkf/emacs-ipython-notebook/wiki/Screenshots) from the original repo now unmaintained. – Tu Do May 29 '15 at 14:04
  • That's a whole lotta text for a pretty simple question. ;-) Would be nice to trim it down a bit. – Malabarba May 29 '15 at 14:39
  • I agree. I was being thorough as other SE sites have thought me. I will trim it a bit asap. – Christophe De Troyer May 29 '15 at 15:19

3 Answers3

12

Well yes there are two python modes: the one which ships with emacs is python.el and the other one is python-mode.el which is indeed this one: https://launchpad.net/python-mode

Your tutorial has been referencing the later one.

Your python-mode setup snippet does the following:

;; by default, the function 'python-mode is associated with
;; the package python.el. The following changes that to python-mode.el:
(autoload 'python-mode "python-mode" "Python Mode." t)

;; open py files with python-mode
(add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode))

;; sets python interpreter mode to be python-mode
(add-to-list 'interpreter-mode-alist '("python" . python-mode))
Andrew Swann
  • 3,436
  • 2
  • 15
  • 43
Adobe
  • 1,859
  • 13
  • 27
  • So do I remove python.el or do I have to combine them? Or is there a specific name for the latter python mode? I want my `init.el` to be portable so removing `python.el` might not be an option. – Christophe De Troyer May 29 '15 at 13:22
  • Just install python-mode.el and set it up with the snippet you posted. (or read its README) – Adobe May 29 '15 at 13:28
  • I understand what the command does, in combination with this answer: http://superuser.com/questions/108233/how-can-i-prevent-the-python-el-that-ships-with-emacs-23-from-ever-loading Thank you for elaborating. – Christophe De Troyer May 29 '15 at 13:30
8

As a maintainer of python-mode.el and in addition to answer by @Adobe: python-mode.el does not unload commands from python.el - both are available. Due to name of python-mode-map --which is used by both and can't be changed without breaking a lot of things-- keybindings and menu are delivered from the last one loaded.

C-c | calls py-execute-region and works nicely here. Maybe python.el was loaded afterwards and the binding gone. Calling the command via M-x might be an option than. In case of trouble, please consider a bug-report at https://bugs.launchpad.net/python-mode

Andreas Röhler
  • 1,894
  • 10
  • 10
1

Despite some people using this package, and despite the fact that it seems to be the first Python supporting library for Emacs, I recommended to the author to rename the feature of this library, maybe to something like 'py-mode'.

As it stand, as soon as you download the python-mode.el file and it sits inside your ~/.emacs.d/elpa directory it will be used by Emacs over the python.el that is now packaged with Emacs.

The confusion comes from the clash of these 2 files:

  • The python.el file that is packaged with Emacs provide the feature 'python-mode and the major mode command 'python-mode.
  • The python-mode.el file you can get from MELPA provides the same 'python-mode feature and major mode.

I wish this situation would not be allowed by package managers and since python.el is now distributed with Emacs I think the authors of python-mode should rename, at the minimum, their feature and probably their file.

PRouleau
  • 744
  • 3
  • 10