📜 ⬆️ ⬇️

Emacs and Python, Python and Emacs

Talk about “Python best IDE - does it really exist?” Often occurs on the web. For example, a recent Q & A in the Python community group on LinkedIn (unfortunately, viewing is possible only 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 certain settings. For Vim, I don’t know, but for Emacs, you need to spend at least 6 hours of searching the Internet and experimenting before you get a comfortable and (very) functional environment for Python.
')
As for me, for a very long time, I worked with Eclipse + PyDev - a workable solution. But after the final move from Windows to Ubuntu (to put Liberation fonts in Ubunt to everyone!), I decided to finish what I started long ago - start using Emacs not only as a GTD organizer, IRC client and editor “when I don’t want to run Eclipse”.

The data history of six hours under habrakat (history assumes that readers are at least familiar with Emacs and Python, and also carries a Ubuntu (Debian) -specific hue when it comes to installing packages).



Emacs python mode


Emacs versions 21 and older have built-in Python programming mode, called the python.el module file name. There is also an independent project that provides approximately the same functionality. Compare both solutions.

Built in python.el


Good news 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:

  1. Syntax highlighting code.
  2. Automatic indentation.
  3. The ability to run an internal (in Emacs) interpreter.
  4. Run the edited code in the internal interpreter (both the entire buffer and the selected part of it).
  5. Any completion.
  6. Convenient commenting / uncommenting code.
  7. Interaction with the Python help system.


Independent python-mode.el


After walking around the Internet for a while on the topic “python-mode.el vs. python.el, ”I concluded that most of the community uses the python-mode.el module . The main reasons for this are quite clear to themselves in this thread on the forum velocityreviews . Of the main can be identified:

  1. Poor "embeddedness" of the internal interpreter in python.el.
  2. Absence of class browser in python.el.
  3. “It's not that look & feel like the good old python-mode.el” - the community is picky :)


Actually there are not so many differences and right away they will not be noticeable - it takes some time to work there and there to find them. But I still put python-mode.el: sudo aptitude install python-mode and business - even .emacs do not need to be edited.

So, we consider that the mode is chosen, we move on after a couple of Emacs screenshots in Python-Mode:





The observant probably noticed that I have ttf fonts in Emacs? This will be a small next post. You may also notice that errors are highlighted on the fly.

Error highlighting on the fly - pylint


Emacs has 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 by default Python is not supported. We correct this omission:

In the console:
sudo aptitude install pylint pymacs

These packages are required for flymake in python-mode. pylint - for checking code for errors, pymacs provides two-way communication 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 sources for errors by calling pylint.

Next, in your .emacs (usually ~ / .emacs, but ordered people have a small hierarchy of files, such as programming.el, python.el, common.el, which are loaded into .emacs using load-file), 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 includes flymake mode by default for all new buffers using python-mode. If you do not write it, to enable flymake you will need to execute Mx flymake-mode in the required buffer.

Everything, error highlighting should earn.

Refactoring and completion of the Python code


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 following lines to the .emacs file:

 (require 'pymacs)
 (pymacs-load "ropemacs" "rope-")


It is possible that you noticed in the process that both the rope and ropemacs packages are regular python modules. And the expansion of Emacs functionality with the help of ropemacs occurs via pymacs - which links EmacsLISP and Python.

We look at the result (pay attention to the menu - the “Rope” item appeared there, through which most of the ropemacs functions are available):

Completion (triggered by pressing M- /):


Refactoring (using Rename as an example):


In fact, this is not the best completion that you can get in Emacs for Python, but let's do it 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 - so with all that I’ve got in the end, I enjoy working my fingers on the keyboard, I completely forgot about the mouse, the minibuffer drives… However, I have serious concerns that those who have not yet used Emacs, but would like to have serious problems - it's like having Windows on Linux, very similar sensations. The buttons are weird, the cursor is moving too surprisingly, some buffers, windows ... I’m writing for these readers - if you really want to try Emacs (it's not for nothing that there are so many conversations about it) - go through Emacs tutorial first, then play with it for a week in the evenings and only then start it in your workflow - it will be difficult to break the habit. I myself was skeptical about this “editor” six months ago - and now he works for me from switching on and off the computer, replacing about a dozen programs (organizer, diary, IDE for different languages, just an editor, jabber and IRC client, tools for writing and publishing documentation in html and not only file manager) - and it is not imposed, it is just more convenient for them.

Thank you all for your attention, I hope the information will be useful for you. And yes - this is my first publication on Habré, if there are mistakes, write in comments, I will consider for the future.

Sources and useful links (many from EmacsWiki on Python-Mode)


EmacsWiki on Python-Mode
Choose between python.el and python-mode.el
Bazaar-repository of the python-mode.el project
Enabling completion in python-mode
Use Cc! to run ipython instead of python
Press F1 to check the code with the pylint
Excellent code completion and python code refactoring
Debugging python code in Emacs: PdbNotes
Python completion with pycomplete
And more about completion

Source: https://habr.com/ru/post/46350/


All Articles