Always check PEP8 while editing Python source code in Emacs

Yesterday wrote about the Python coding convention PEP8 ..

While writing Python source code in Emacs, it would be even more useful if PEP8 conformance was constantly checked and visualized.

With Emacs Lisp introduced here, PEP8 conformance violations are always visualized on Emacs as shown below.

1.png

Also, only while editing Python, you will be able to see the PEP8 check results in the minibuffer with certain key bindings.

Prerequisite Emacs Lisp

First, prepare Emacs Lisp, which is a prerequisite for developing Python with Emacs.

python-mode.el

There are several major modes in Python, but my recommendation is python-mode.el.

python-mode.el http://www.emacswiki.org/emacs/PythonProgrammingInEmacs

This is the most feature rich.

flymake-cursor.el

Works with flymake, which is the default in Emacs 23 and later, to display a flymake error on the current line in the minibuffer.

flymake-cursor.el https://github.com/illusori/emacs-flymake-cursor

Set Python mode

In the article yesterday (when (load "python-pep8") (define-key global-map "\ Cc \ p"'python-pep8)) However, this causes pep8 to be checked by Cc p while editing any file. So it's a good idea to use the python-mode hook and enable it only while editing Python.

With this in mind, write the settings for python-mode in Emacs Lisp as follows:

;; python-Load mode
(when (autoload 'python-mode "python-mode" "Python editing mode." t)
  ;; python-python only in mode-Enable key bindings for pep8
  (setq python-mode-hook
  (function (lambda ()
    (local-set-key "\C-c\ p" 'python-pep8))))
  (setq auto-mode-alist (cons '("\\.py$" . python-mode) auto-mode-alist))
  (setq interpreter-mode-alist (cons '("python" . python-mode)
                                     interpreter-mode-alist)))

;; flymake for python
(add-hook 'python-mode-hook 'flymake-find-file-hook)
(when (load "flymake" t)
  (defun flymake-pyflakes-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))))
      ;;Specify your own shell script introduced yesterday
      ;;Please edit according to each environment
      (list "/usr/local/sbin/pyck"  (list local-file))))
  (add-to-list 'flymake-allowed-file-name-masks
               '("\\.py\\'" flymake-pyflakes-init)))
(load-library "flymake-cursor")

This is the state introduced at the beginning. The error you want to ignore can be specified in the shell script called from above and in python-pep8.

To ignore the error in python-pep8.el, edit Emacs Lisp and pass it as an option in pep8 as follows:

(defcustom python-pep8-options '("--ignore=E302")
  "Options to pass to pep8.py"
  :type '(repeat string)
  :group 'python-pep8)

Summary

The settings of Emacs are different for each person, but if you create an environment that is easy for each person to develop based on this content, you will be happier.

Recommended Posts

Always check PEP8 while editing Python source code in Emacs
Check python code styles using pep8
I want to write in Python! (1) Code format check
Ruby, Python code fragment execution of selection in Emacs
Remove one-line comments containing Japanese from source code in Python
Source code character code check script
Generate QR code in Python
Character code learned in Python
Interrupt registration procedure as seen in the source code
View the implementation source code in iPython
Flow from source code to creating executable
Syntax highlighting source code in PowerPoint / Keynote
File processing in Python
Multithreaded processing in python
Text processing in Python
Queue processing in Python
Remove one-line comments containing Japanese from source code in Python
Always check PEP8 while editing Python source code in Emacs
[PEP8] Take over the Python source code and write it neatly
Check for memory leaks in Python
Check for external commands in python
[Python] Generate QR code in memory
Automatically format Python code in Vim
Check and move directories in Python
[Python] Read the Flask source code
Code tests around time in Python
How to automatically check if the code you wrote in Google Colaboratory corresponds to the python coding standard "pep8"
What Emacs users should know when writing python code in Sublime Text
Fourier series verification code written in Python
View the implementation source code in iPython
Check the behavior of destructor in Python
Check and receive Serial port in Python (Port check)
Getting Python source code metrics using radon
Check if the URL exists in Python
Automatically generate Python Docstring Comment in Emacs
How to check opencv version in python
python> coding guide> PEP 0008 --Style Guide for Python Code
Find and check inverse matrix in Python
Get the EDINET code list in Python
Syntax highlighting source code in PowerPoint / Keynote
Notes on using code formatter in Python
Difference in writing method to read external source code between Ruby and Python