
Emacs and Python, Python and Emacs
There is often talk on the net about the "Python best IDE - does it really exist?" For example, the recent Q&A in the Python community group on LinkedIn (unfortunately, viewing is only possible for users of the group). In short, the world revolves around:
If the first four solutions provide an IDE for Python right out of the box, then the last two require specific settings. I don’t know for Vim, but for Emacs you need to spend at least 6 hours searching the Internet and experimenting before you get a convenient and (very) functional environment for Python.
As for me, for a very long time I worked with Eclipse + PyDev - a fully functional solution. But after the final move from Windows to Ubuntu (everyone should install Liberation fonts in Ubunt!), I decided to finish what I started a long time ago - to start using Emacs not only as a GTD organizer, IRC client and editor “when I don’t want to run Eclipse”.
A six-hour history of data under a habracat (history assumes that readers are at least slightly familiar with Emacs and Python, and also have Ubuntu (Debian) -specific hue when it comes to installing packages).
Emacs versions 21 and older have a built-in Python programming mode called by the name of the python.el module file. There is an independent project that provides approximately the same functionality. Compare both solutions.
The good news is for those who just need a convenient and functional text editor for python scripts - you can do nothing that is described below, python.el provides a good set of features right out of the box:
After some time exploring the expanses of the Internet on the topic “python-mode.el vs. python.el ”, I came to the conclusion that most of the community uses the python-mode.el module . The main reasons for this are quite set forth in this thread on the forum velocityreviews . Of the main ones, one can distinguish
There are not so many differences, and they won’t be immediately noticeable - you need to work there and there for a while to find them. But I nevertheless set python-mode.el:
So, we believe that we have chosen the mode, go ahead after a couple of screenshots of Emacs in Python-Mode: Have observers probably noticed that I have ttf fonts in Emacs? This will be a small next post. You can also notice that errors are highlighted on the fly.


Emacs has an excellent flymake-mode, which allows you to highlight on-the-fly all compilation errors (as well as warnings and messages about bad programming style). The trouble is that Python is not supported by default. We fix this omission:
In the console:
These packages are required for flymake in python-mode. pylint - to check the code for errors, pymacs provides two-way interaction between Python and EmacsLISP.
Create the following script in your PATH (I created in ~ / bin), name it epylint:
This script will check our source for errors by calling pylint.
Next, in your .emacs (usually ~ / .emacs, but ordered people have a small hierarchy of files, like programming.el, python.el, common.el, which are loaded into .emacs using load-file) we add the lines:
The last line is optional - it turns on the default flymake mode for all new buffers using python-mode. If you do not write it, to enable flymake, you will need to execute it
Everything, error highlighting should work.
We go to SourceForge hosting of Rope and Ropemacs projects , download rope and ropemacs, install -
After that, add the lines to the .emacs file:
It is possible that you noticed in the process that both rope and ropemacs are regular python modules. And the extension of Emacs functionality with ropemacs occurs through pymacs - which connects EmacsLISP and Python.
We look at the result (pay attention to the menu - there appeared the “Rope” item, through which most of the functions of ropemacs are available):
Completion (triggered by pressing M- /): Refactoring (using Rename as an example): Actually, this is not the best completion that you can get in Emacs for Python, but next time :) I promise to tell you about my further research.


I’m used to Emacs, its keyboard shortcuts and the general paradigm - therefore, with all that it turned out in the end, I enjoy working with it - fingers fly around the keyboard, I completely forget about the mouse, the minibuffer drives ... However, I have serious concerns, that those who haven’t used Emacs yet, but would like to have serious problems - it’s like after Windows to transfer to Linux, very similar feelings. The buttons are strange, the cursor is moving too tricky, some kind of buffers, windows ... I’m writing for such readers - if you really want to try Emacs (there is so much talk about it) - go through the Emacs tutorial first, then play with it for a week or so in the evenings then launch it into your workflow - it will be difficult to break the habit.
Thank you all for your attention, I hope the information will be useful to you. And yes - this is my first publication on the hub, if there are oversights - write in the comments, I will take into account the future.
EmacsWiki on Python-Mode
Choosing between python.el and python-mode.el
Bazaar project repository python-mode.el
Enabling completion in python-mode
Using Cc! to start ipython instead of python
Press F1 to check the code with pylint
Excellent code completion and refactoring python code
Debugging python code in Emacs: PdbNotes
Python completion with pycomplete
And about completion
- Eclipse + PyDev - It seems that the most popular of the free solutions.
- Komodo IDE - reviews a great tool, but not free.
- WingIDE is the best autocompletion for Python, but again non-free.
- Eric - IDE on Qt and QScintilla, tried only on Windows, apparently tried it out - it seemed uncomfortable. If anyone has useful information and links - please share in the comments.
- NetBeans 6.5 - Native Python support is getting ready for release in this IDE version 6.5, we look forward to it!
- Vim - for fans of Vim nothing better to be, of course, can not - after file processing, really.
- Emacs - similar to Vim - for users who spend a lot of time in Emacs, it gives the best user-experience when developing in any language.
If the first four solutions provide an IDE for Python right out of the box, then the last two require specific settings. I don’t know for Vim, but for Emacs you need to spend at least 6 hours searching the Internet and experimenting before you get a convenient and (very) functional environment for Python.
As for me, for a very long time I worked with Eclipse + PyDev - a fully functional solution. But after the final move from Windows to Ubuntu (everyone should install Liberation fonts in Ubunt!), I decided to finish what I started a long time ago - to start using Emacs not only as a GTD organizer, IRC client and editor “when I don’t want to run Eclipse”.
A six-hour history of data under a habracat (history assumes that readers are at least slightly familiar with Emacs and Python, and also have Ubuntu (Debian) -specific hue when it comes to installing packages).
Emacs Python-Mode
Emacs versions 21 and older have a built-in Python programming mode called by the name of the python.el module file. There is an independent project that provides approximately the same functionality. Compare both solutions.
Built-in python.el
The good news is for those who just need a convenient and functional text editor for python scripts - you can do nothing that is described below, python.el provides a good set of features right out of the box:
- Syntax highlighting code.
- Automatic indentation.
- Ability to run the internal (in Emacs) interpreter.
- Running editable code in the internal interpreter (both the entire buffer and its selected part).
- Some kind of completion.
- Convenient code commenting / uncommenting functions.
- Interaction with the Python help system.
Independent python-mode.el
After some time exploring the expanses of the Internet on the topic “python-mode.el vs. python.el ”, I came to the conclusion that most of the community uses the python-mode.el module . The main reasons for this are quite set forth in this thread on the forum velocityreviews . Of the main ones, one can distinguish
- Poor "inline" of the internal interpreter in python.el.
- Lack of class explorer in python.el.
- “Not at all the look & feel like the good old python-mode.el” - the community is picky :)
There are not so many differences, and they won’t be immediately noticeable - you need to work there and there for a while to find them. But I nevertheless set python-mode.el:
sudo aptitude install python-mode
and business - even .emacs does not need to be edited. So, we believe that we have chosen the mode, go ahead after a couple of screenshots of Emacs in Python-Mode: Have observers probably noticed that I have ttf fonts in Emacs? This will be a small next post. You can also notice that errors are highlighted on the fly.


Highlight errors on the fly - pylint
Emacs has an excellent flymake-mode, which allows you to highlight on-the-fly all compilation errors (as well as warnings and messages about bad programming style). The trouble is that Python is not supported by default. We fix this omission:
In the console:
sudo aptitude install pylint pymacs
These packages are required for flymake in python-mode. pylint - to check the code for errors, pymacs provides two-way interaction between Python and EmacsLISP.
Create the following script in your PATH (I created in ~ / bin), name it epylint:
#! / usr / bin / env python import re import sys from subprocess import * p = Popen ("pylint -f parseable -rn --disable-msg-cat = C, R% s"% sys.argv [1], shell = True, stdout = PIPE) .stdout for line in p.readlines (): match = re.search ("\\ [([WE]) (, (. +?))? \\]", line) if match: kind = match.group (1) func = match.group (3) if kind == "W": msg = "Warning" else: msg = "Error" if func: line = re.sub ("\\ [([WE]) (, (. +?))? \\]", "% s (% s):"% (msg, func), line) else: line = re.sub ("\\ [([WE])? \\]", "% s:"% msg, line) print line, p.close ()
This script will check our source for errors by calling pylint.
Next, in your .emacs (usually ~ / .emacs, but ordered people have a small hierarchy of files, like programming.el, python.el, common.el, which are loaded into .emacs using load-file) we add the lines:
(when (load "flymake" t) (defun flymake-pylint-init () (let * ((temp-file (flymake-init-create-temp-buffer-copy 'flymake-create-temp-inplace)) (local-file (file-relative-name temp-file (file-name-directory buffer-file-name)))) (list "epylint" (list local-file)))) (add-to-list 'flymake-allowed-file-name-masks '("\\. py \\'" flymake-pylint-init))) (add-hook 'python-mode-hook' flymake-mode)
The last line is optional - it turns on the default flymake mode for all new buffers using python-mode. If you do not write it, to enable flymake, you will need to execute it
M-x flymake-mode
in the desired buffer. Everything, error highlighting should work.
Refactoring and completing Python code
We go to SourceForge hosting of Rope and Ropemacs projects , download rope and ropemacs, install -
sudo python setup.py install
- both packages. After that, add the lines to the .emacs file:
(require 'pymacs) (pymacs-load "ropemacs" "rope-")
It is possible that you noticed in the process that both rope and ropemacs are regular python modules. And the extension of Emacs functionality with ropemacs occurs through pymacs - which connects EmacsLISP and Python.
We look at the result (pay attention to the menu - there appeared the “Rope” item, through which most of the functions of ropemacs are available):
Completion (triggered by pressing M- /): Refactoring (using Rename as an example): Actually, this is not the best completion that you can get in Emacs for Python, but next time :) I promise to tell you about my further research.


For newbies to Emacs
I’m used to Emacs, its keyboard shortcuts and the general paradigm - therefore, with all that it turned out in the end, I enjoy working with it - fingers fly around the keyboard, I completely forget about the mouse, the minibuffer drives ... However, I have serious concerns, that those who haven’t used Emacs yet, but would like to have serious problems - it’s like after Windows to transfer to Linux, very similar feelings. The buttons are strange, the cursor is moving too tricky, some kind of buffers, windows ... I’m writing for such readers - if you really want to try Emacs (there is so much talk about it) - go through the Emacs tutorial first, then play with it for a week or so in the evenings then launch it into your workflow - it will be difficult to break the habit.
Thank you all for your attention, I hope the information will be useful to you. And yes - this is my first publication on the hub, if there are oversights - write in the comments, I will take into account the future.
Sources and useful links (many of the EmacsWiki on Python-Mode)
EmacsWiki on Python-Mode
Choosing between python.el and python-mode.el
Bazaar project repository python-mode.el
Enabling completion in python-mode
Using Cc! to start ipython instead of python
Press F1 to check the code with pylint
Excellent code completion and refactoring python code
Debugging python code in Emacs: PdbNotes
Python completion with pycomplete
And about completion