AWS / PHP / Python ちょいメモ

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

Mac に pythonz 入れる前にやっておくこと

direnv構築 していて遭遇した問題の原因がわかった。homebrew版のpythonが必要なのではなく、「homebrewでopensslを事前にインストールしておく必要がある」が正解だった。

先に openssl を入れておかないと、pythonz で落としてきたpythonのBuild時に openssl無しのBuildが行われる。前回は、この順番がおかしかったみたい。



(2016/12/06 追記) ここで紹介した手順では導入できなくなっるようなので、修正手順を別記事にまとめました。



手順の再確認

homebrew direnv には homebrew python が必要? - AWS / PHP / Python ちょいメモ でメモしてた手順を修正。

手順(間違い):

  • direnv (by homebrew)
  • pip (on Mac OS X標準 python)
  • pythonz 1.11.0 (手動)
  • virtualenv (by pip)
  • openssl (by homebew)

手順(正解):

  1. openssl (by homebew)
    • brew install openssl
    • brew link openssl --force
    • 確認:$ which openssl | xargs ls -al
      • lrwxr-xr-x 1 me admin 36 4 13 10:35 /usr/local/bin/openssl -> ../Cellar/openssl/1.0.2g/bin/openssl
  2. pip (on Mac OS X標準 python)
  3. pythonz 1.11.0 (手動)
  4. virtualenv (by pip)
  5. direnv (by homebrew OR make install [要 golang] )


問題のエラー抜粋:

$ 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()
...

Traceback をちゃんと追いかけてみると、次のような感じ。

  • direnv: loading .envrc
    • direnv対象のフォルダを初期化しようとして
  • Running virtualenv with interpreter /Users/me/.pythonz/pythons/CPython-2.7.11/bin/python
    • virtualenv を pythonz で管理されてる 2.7.11 で動作させるよ
  • Complete output from command /Users/me/...on-2.7.11/bin/python - setuptools pip wheel
    • pythonz の 2.7.11 で setuptools pip wheel を処理仕様とした出力
  • 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
    • virtualenv_support (ホストで動作してる pip に virtualenv 入れた時に入る) の pip.whl を処理するときに
  • ImportError: cannot import name HTTPSHandler
    • HTTPSHandler が import できんかった

wheelされてる同種のファイルを見てみる:

/Library/Python/2.7/site-packages/pip/_vendor/distlib/compat.py
...
31 from urllib2 import (Request, urlopen, URLError, HTTPError,
32 HTTPBasicAuthHandler, HTTPPasswordMgr,
33 HTTPSHandler, HTTPHandler, HTTPRedirectHandler,
34 build_opener)

エラーが起きたpythonで from urllib2 import HTTPSHandler すると、同じエラーが出た。前回、解決したと思ってたのは、opensslインストール後にいれたpythonで構築してたからか。。。

教訓

パッケージマネージャー使って管理してても、環境によって コンパイル オプション 変わるから、ちゃんと確認しましょう。
ってことですね。OSの違い(Mac とか Linuxとか) を吸収してくれてるところ(configure ?)を、盲目的に依存してはいかんと。