4
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

emacs + elpy + pyenv-virtualenv でシステムの python が使われてしまう

Last updated at Posted at 2017-08-12

どうも elpy のもろもろの補完が頭悪いと思っていたら、 python 環境が pyenv のものではなくてシステムのものが使われていた。というメモ。

elpy?

python 開発の IDE 的な機能をやってくれる賢いやつ。

  • オブジェクトのメンバやそのたいろんなものを補完
  • メソッドやクラスのドキュメントを見る
  • 定義に飛ぶ

などなどやってくれる。
それらをやってもらうには、 pip install jedi で入れた jedi とかいうやつらがよしなにやってくれるらしい。

現状の確認

M-x elpy-config でみれる

   1 Elpy Configuration
   2
   3 Virtualenv........: ml_test (/home/harumitsu.nobuta/.pyenv/versions/3.5.2/envs/ml_test)
   4 RPC Python........: 2.7.12 (/home/harumitsu.nobuta/.pyenv/shims/python)
   5 Interactive Python: python (/home/harumitsu.nobuta/.pyenv/shims/python)
   6 Emacs.............: 24.5.1
   7 Elpy..............: 1.15.1
   8 Jedi..............: Not found (0.10.2 available)
   9 Rope..............: Not found (0.10.5 available)
  10 Importmagic.......: Not found (0.1.7 available)
  11 Autopep8..........: Not found (1.3.2 available)
  12 Yapf..............: Not found (0.16.3 available)
  13 Syntax checker....: flake8 (/home/harumitsu.nobuta/.pyenv/shims/flake8)
  14

Virtualenv は 3.5.2 になってるのに、なぜか RPC Python は 2.7.12 に。。。
2.7.12 はシステムの python コマンドを叩いたときのものなので、何故か pyenv がうまく動いていないくてシステムのものを参照させてしまっている。

当然、 jedi とかを入れた環境は virtual_env の ml_test なのでシステムには入っていない。よって補完などがアホになる。

原因

https://github.com/jorgenschaefer/elpy/issues/803
作者がむりぽっていってるからむりぽい。

対応策

https://github.com/gregnewman/emacs.d/blob/master/greg.org
を参考に、自前で .python-version を読みよしなに設定するスクリプトを書く。
意外とかんたんにできた。

1. 以下の elisp を作る

.emacs.d/elisp/set-pyenv-version-path.el
(defun set-pyenv-version-path ()
  "Automatically activates pyenv version if .python-version file exists."
  (f-traverse-upwards
   (lambda (path)
     (let ((pyenv-version-path (f-expand ".python-version" path)))
       (if (f-exists? pyenv-version-path)
           (pyenv-mode-set (s-trim (f-read-text pyenv-version-path 'utf-8))))))))
(provide 'set-pyenv-version-path)

2. init.el に追記

もしくは .emacs に追記。
https://github.com/halhorn/.emacs.d/blob/master/init.el#L193

.emacs.d/init.el
(require 'set-pyenv-version-path)
(add-hook 'find-file-hook 'set-pyenv-version-path)
(add-to-list 'exec-path "~/.pyenv/shims")
4
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?