AWS / PHP / Python ちょいメモ

amazon web service , PHP, Python を使ったときのメモ。日本語でググってもわからなかった事を中心に。

homebrew direnv には homebrew python が必要?

(4/27 追記) homebew版 python が必要ではなく、direnv で使ってた pythonz で pythonを入れる前に homebew版 openssl が必要だったのが問題点でした。
Mac に pythonz 入れる前にやっておくこと - AWS / PHP / Python ちょいメモ

      • -

direnvをよく目にするので使ってみようと思ったものの、手元のMacで使うのに時間がかかった。

結論としては homebrew でインストールした direnv 使うには、母体となる python も homebrew で入れる必要あり?というところに落ちました。

母体はオリジナルにしておきたかったので direnv 使おうと思ったんだけど、若干本末転倒?でもディレクトリ移動するだけで使えるのは便利ですね。

環境

$ sw_vers
ProductName: Mac OS X
ProductVersion: 10.11.3
BuildVersion: 15D21

$ brew --version
Homebrew 0.9.9 (git revision 4e8c; last commit 2016-04-12)
Homebrew/homebrew-core (git revision 5fd7; last commit 2016-04-12)

最初にトライ

次の構成でいけると思ってやってみた。

で .envrc を書いたフォルダへ入ると direnvが動作して。。。落ちる

$ cd chickadee
direnv: loading .envrc
Running virtualenv with interpreter /Users/me/.pythonz/pythons/CPython-2.7.11/bin/python
New python executable in /Users/me/repo/chickadee/.direnv/python-2.7.11/bin/python
Installing setuptools, pip, wheel...
Complete output from command /Users/me/...on-2.7.11/bin/python - setuptools pip wheel:
Traceback (most recent call last):
File "", line 7, in
File "/Library/Python/2.7/site-packages/virtualenv_support/pip-8.1.1-py2.py3-none-any.whl/pip/__init__.py", line 16, in
File "/Library/Python/2.7/site-packages/virtualenv_support/pip-8.1.1-py2.py3-none-any.whl/pip/vcs/subversion.py", line 9, in
File "/Library/Python/2.7/site-packages/virtualenv_support/pip-8.1.1-py2.py3-none-any.whl/pip/index.py", line 30, in
File "/Library/Python/2.7/site-packages/virtualenv_support/pip-8.1.1-py2.py3-none-any.whl/pip/wheel.py", line 39, in
File "/Library/Python/2.7/site-packages/virtualenv_support/pip-8.1.1-py2.py3-none-any.whl/pip/_vendor/distlib/scripts.py", line 14, in
File "/Library/Python/2.7/site-packages/virtualenv_support/pip-8.1.1-py2.py3-none-any.whl/pip/_vendor/distlib/compat.py", line 31, in
ImportError: cannot import name HTTPSHandler

                                                                              • -

...Installing setuptools, pip, wheel...done.
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/virtualenv.py", line 2316, in
main()
File "/Library/Python/2.7/site-packages/virtualenv.py", line 708, in main
symlink=options.symlink)
File "/Library/Python/2.7/site-packages/virtualenv.py", line 941, in create_environment
download=download,
File "/Library/Python/2.7/site-packages/virtualenv.py", line 897, in install_wheel
call_subprocess(cmd, show_stdout=False, extra_env=env, stdin=SCRIPT)
File "/Library/Python/2.7/site-packages/virtualenv.py", line 792, in call_subprocess
% (cmd_desc, proc.returncode))
OSError: Command /Users/me/...on-2.7.11/bin/python - setuptools pip wheel failed with error code 1
direnv: error exit status 1

HTTPSHandler は openssl がというエントリーを見つけたので、homebrewでインストールしてみるが解決せず。

brew install openssl
brew link --overwrite --dry-run openssl # safety first.
brew link openssl --overwrite

解決方法

仮想環境配下だけ pythonz の環境を使おうとしていたのですが、どうやら direnv を homebrew からいれて構築していく場合は、母体となる python もhomebew からインストールが必要みたい。

次の方が、同じ状況になってたので、

解決した方法を参照して、真似してみたら、

動いた!

$ direnv allow chickadee/.envrc
$ cd chickadee
direnv: loading .envrc
Running virtualenv with interpreter /Users/me/.pythonz/pythons/CPython-2.7.8/bin/python
New python executable in /Users/me/repo/chickadee/.direnv/python-2.7.8/bin/python
Installing setuptools, pip, wheel...direnv: ([direnv export bash]) is taking a while to execute. Use CTRL-C to give up.
done.
direnv: export +VIRTUAL_ENV ~PATH
$ python -V
Python 2.7.8

その他

なるべく標準の環境はさわりたくなかったなぁ。direnv を 公式の手順通り make すれば避けれるのかもしれない。また今度やろうっと。いまはいいのだけど、OSアップグレードした時におかしくなりそうな点が怖いのよね。Appleは、ユーザーが自由にやってることには、あまり寛容ではないとの印象なので。

複数バージョンのpython使い分ける必要がなく、pipでの依存ライブラリだけ切り分けたい時は virtualenv + virtualenvrapper の方がライトだな。

以前、メモったエントリー。今読むと、よく理解出来てなかったことがわかる。。

さて、開発しようっと。