Skip to content

Latest commit

 

History

History
245 lines (202 loc) · 8.51 KB

31_python.org

File metadata and controls

245 lines (202 loc) · 8.51 KB

Python

Python 環境.

elpy

総合環境

;;; Elpy を有効化
;; (require 'elpy)
;; (elpy-enable)
;;; 使用する Anaconda の仮想環境を設定
;; (defvar venv-default "~/anaconda3/envs/py36")
;;; virtualenv を使っているなら次のようなパス
;; (defvar venv-default "~/.virtualenvs/hoge")
;;; デフォルト環境を有効化
;; (pyvenv-activate venv-default)
;;; REPL 環境に IPython を使う
;; (elpy-use-ipython)
;;; 自動補完のバックエンドとして Rope か Jedi を選択
;; (setq elpy-rpc-backend "jedi")

anaconda-mode

シンタックスハイライト

python.el は公式, python-mode.el は Python コミュニティによって作成されたもの.

python

Emacs にデフォルトではいっているやつ.

python-mode

Python コミュニティによって作成されたもの.

ショートカットがまとまっている.

(if linux-p
    (setq py-install-directory "~/.emacs.d/el-get/repo/python-mode"))
(if windows-p
    (setq py-install-directory "C:\cygwin64\home\tsu-nera\dotfiles\.emacs.d\el-get\repo\python-mode"))
(if windows-p
    (setq python-shell-interpreter "c:/Python34/python.exe"))
(add-to-list 'load-path py-install-directory)
(require 'python-mode)
(add-hook 'python-mode-hook
          '(lambda ()
             (setq indent-tabs-mode nil)
             (setq indent-level 4)
             (setq python-indent 4)
             (setq tab-width 4)
             (setq imenu-create-index-function 'python-imenu-create-index)
             ))

補完

jedi

Python のオムニ補完 (かしこい補完) をしてくれる.

初回利用時には、M-x jedi:install-server

他の環境で動かないので、封印

(add-hook 'python-mode-hook 'jedi:setup)
(add-hook 'python-mode-hook 'jedi:ac-setup) ;; auto-complete のソースに追加
(setq jedi:complete-on-dot t)

jump もできる.

  • C-c . jedi:goto-definition
  • C-c , jedi:goto-definition-pop-marker

リファクタリング

参考リンク:

Ropemacs

Python 用のリファクタリングツール.

うごかないよーん. python3 で動くのかな??

;; (autoload 'pymacs-apply "pymacs")
;; (autoload 'pymacs-call "pymacs")
;; (autoload 'pymacs-eval "pymacs" nil t)
;; (autoload 'pymacs-exec "pymacs" nil t)
;; (autoload 'pymacs-load "pymacs" nil t)

;; (require 'pymacs)
;; (pymacs-load "ropemacs" "rope-")
;; (setq ropemacs-enable-autoimport t)

traad

コーディング支援

helm-pydoc

pydoc の helm i/f.

(use-package helm-pydoc
  :if linux-p
  :init
  (define-key python-mode-map (kbd "C-c C-d") 'helm-pydoc))

quickrun

quickrun を利用すれば, python コードを Emacs から実行可能.

システムの version が 3 で, Emacs からは 2 を利用したいときは, 以下のようにすれば実行コマンドをオーバライドできる.

;; (eval-after-load 'quickrun
;;  '(progn
;;     (quickrun-add-command "python" '((:command . "python2"))
;;			   :override t)))

iPython

インタラクティブなデバッグ環境.

静的解析

flycheck

以下が利用できる(らしい) .

;; (add-hook 'python-mode-hook 'my/turn-on-flycheck-mode)

flake8 をつかってみようか. #noqa をつけると、エラー対象外.

pip install flake8

flake8-python2 を利用するようにする.

-> ArchLinux から Ubuntu にしたら動かなくなっちゃった・・・ Python 書く日まで封印

;; (flycheck-define-checker python2-flake8
;;  "A Python syntax and style checker using Flake8.
;;   Requires Flake8 2.0 or newer. See URL
;;  `https://pypi.python.org/pypi/flake8'."
;;  :command ("flake8-python2"
;;            "--format=default"
;;            (config-file "--config" flycheck-flake8rc)
;;            (option "--max-complexity" flycheck-flake8-maximum-complexity nil
;;                    flycheck-option-int)
;;            (option "--max-line-length" flycheck-flake8-maximum-line-length nil
;;                    flycheck-option-int)
;;            source)
;;  :error-filter (lambda (errors)
;;                  (let ((errors (flycheck-sanitize-errors errors)))
;;                    (mapc #'flycheck-flake8-fix-error-level errors)
;;                    errors))
;;  :error-patterns
;;  ((warning line-start
;;            (file-name) ":" line ":" (optional column ":") " "
;;            (id (one-or-more (any alpha)) (one-or-more digit)) " "
;;            (message (one-or-more not-newline))
;;            line-end))
;;  :modes python-mode)
;;
;; (add-hook 'python-mode-hook 
;;          (lambda () (flycheck-select-checker 'python2-flake8)))

タグジャンプ

Pygments

Pygments というのをつかうらしい.

おっと、3.x は現在未サポート. 2015/07

etags

これが使えた!

ipython-notebook

Emacs から jupyter notebook が利用できる。

ものすごくロードに時間がかかる。。。

;; (require 'ein)
;; (require 'ein-loaddefs)
;; (require 'ein-notebook) 
;; (require 'ein-subpackages)

virtual environment

conda

(use-package conda
  :config
  ;; if you want interactive shell support, include:
  (conda-env-initialize-interactive-shells)
  ;; if you want eshell support, include:
  (conda-env-initialize-eshell)
  ;; if you want auto-activation (see below for details), include:
  (conda-env-autoactivate-mode t)
  (custom-set-variables
   '(conda-anaconda-home "/home/tsu-nera/anaconda3"))

  (defun conda-remove-path()
    (interactive)
    (delq "/home/tsu-nera/anaconda3/bin" (getenv "PATH"))
    )
  (setq exec-path (delete "/home/tsu-nera/anaconda3/bin" exec-path))
  )

Links