python 開発環境†
- emacs 上での python 開発環境としては以下を導入することが出来る。
- また、 emacs では python コードを編集するための python-mode があるので、これが起動するときのフック python-mode-hook に引っ掛けて動作するようにしていく。
環境構築†
lint の設定(flycheck を使用)†
- flycheck の導入
- flycheck を設定する
formatter の設定†
- pytohn フォーマッタは幾つかあるが、ここでは、以下を前提とした環境構築を行う。
- autopep8 --- フォーマッタ
- isort --- import 文の妥当性を解析する
autopep8 の導入†
- emacs には py-autopep8.el があるので導入する。
isort の導入†
- emacs には py-isort.el があるので導入する。
autopep8 と isort の設定†
- python-mode 固有の設定になるので、ひとまとめにした設定ファイル python-mode.conf.el を作る。
;;; python-mode.conf.el --- settings for python-mode
;;; Commentary:
;;; see also: https://qiita.com/fujimisakari/items/74e32eddb78dff4be585
;;; Code:
(setq python-mode-hook
#'(lambda ()
(if (which-function-mode nil) (which-function-mode)) ; ; 現在の関数名をモードラインに表示
;; formatter
(require 'py-autopep8.conf)
;; clang-format.conf.el で設定したキーに合わせておく
(define-key python-mode-map (kbd "C-c i") 'py-autopep8-region) ; 選択リジョン内のコード整形
(define-key python-mode-map (kbd "C-c u") 'py-autopep8) ; バッファ全体のコード整形
;; import 文を適切な順序に自動で sort する
(require 'py-isort.conf)
;; linter
;; (when (expand-file-name "pylint") ; システム上に pylint があるときだけ
;; ;; flycheck で使う linter の設定
;; ;; flycheck では勝手に linter を探してくれるらしく、パスの通ってない
;; ;; ところに置かれている物を使うのでない限り、以下の linter を指定する設定は不要な模様
;; ; (setq flycheck-python-pylint-executable "pylint") ; for flycheck
;; ; (setq flycheck-python-flake8-executable "flake8") ; for flycheck
;; ; (setq flycheck-python-mypy-executable "mypy") ; for flycheck
;; )
)
)
(provide 'python-mode.conf)
;;; python-mode.conf.el ends here
- これを、${HOME}/.emacs.d/init.el 等から適当に呼ぶようにする。
; python-mode 関係の設定
(require 'python-mode.conf)
Last-modified: 2024-01-22 (月) 00:08:49