brew update -> upgradeしたらbrew側のpythonを利用しなくなった件

※:これはただのメモです。書いてて眠かったので内容が正確かなどの保証はありません

一昨日、homebrew-caskを入れてupdateをかけたら、pyhtonのシンボリックリンクが切れて、brew側のpythonが使えなくなった

If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenv has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python2.7 and that PATH is
set properly.
hiroshi-no-MacBook-Air:~ hiroshi$ python
Python 2.7.5 (default, Aug 25 2013, 00:04:04)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> ^D
hiroshi-no-MacBook-Air:~ hiroshi$ which python
/usr/bin/python
hiroshi-no-MacBook-Air:~ hiroshi$ /usr/local/bin/py
pycolor            pyflakes           python3-config     python3.4m-config
pycolor3           pylupdate4         python3.4          pyuic4
pydoc3             pyrcc4             python3.4-config   pyvenv
pydoc3.4           python3            python3.4m         pyvenv-3.4
hiroshi-no-MacBook-Air:~ hiroshi$ /usr/local/bin/python
-bash: /usr/local/bin/python: No such file or directory
hiroshi-no-MacBook-Air:~ hiroshi$ homebrew install python
-bash: homebrew: command not found
hiroshi-no-MacBook-Air:~ hiroshi$ brew install python
Warning: python-2.7.6_1 already installed, it's just not linked

バージョンが2.7.6_1となっていました。pythonのシンボリックリンクが作成されていないようです。 brew updateした時にpyhtonのアップグレードもあったようで、その時に失敗したような記述が書いてあったような(うろ覚え。ログとか取るべきだった) どうすればいいか調べたもののいまいちわからなかったので、まずdoctorを実行すると

hiroshi-no-MacBook-Air:~ hiroshi$ brew doctor
Warning: Broken symlinks were found. Remove them with `brew prune`:
  /usr/local/lib/python3.4/site-packages/site-packages

Warning: You have unlinked kegs in your Cellar
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built. Run `brew link` on these:

    python

Warning: Your Homebrew is outdated.
You haven't updated for at least 24 hours, this is a long time in brewland!
To update Homebrew, run `brew update`.

一番上のpython 3.4のシンボリックリンクがおかしくなってるのはひとまず置いておいて(なぜそんなふうになってるか不明) doctorでも推奨されたので、brew link pythonを動かす

hiroshi-no-MacBook-Air:~ hiroshi$ brew link python
Linking /usr/local/Cellar/python/2.7.6...
Error: Could not symlink bin/pip-2.7
Target /usr/local/bin/pip-2.7
already exists. You may want to remove it:
  rm /usr/local/bin/pip-2.7

To force the link and overwrite all conflicting files:
  brew link --overwrite python

To list all files that would be deleted:
  brew link --overwrite --dry-run python

pip-2.7のシンボリックリンクがあるということで、エラーになりました。whichで確認すると確かに存在している

rmするかoverwriteしろとのことなので、ovrewriteの--dry-runで状況を確認してみる

hiroshi-no-MacBook-Air:~ hiroshi$ brew link python --overwrite --dry-run
Would remove:
/usr/local/bin/pip-2.7
/usr/local/bin/pip -> /usr/local/Cellar/python/2.7.6_1/bin/pip
/usr/local/bin/easy_install-2.7 -> /usr/local/Cellar/python/2.7.6_1/bin/easy_install-2.7

消される予定になるのがコレらしい。pip-2.7はどうなってしまうのだろうかはともかく実行してみる

hiroshi-no-MacBook-Air:~ hiroshi$ brew link python --overwrite
Linking /usr/local/Cellar/python/2.7.6... 34 symlinks created

ということで、pythonを動かすと

hiroshi-no-MacBook-Air:~ hiroshi$ which python
/usr/local/bin/python
hiroshi-no-MacBook-Air:~ hiroshi$ python

Python 2.7.5 (default, Aug 25 2013, 00:04:04)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> ^D

あれ、変わってない(汗)。ああ、ターミナルを再起動かなと行うと

hiroshi-no-MacBook-Air:~ hiroshi$ python --version
Python 2.7.6
# python —versionで確認すればよかったかw

ひとまず動くようになったようです。

が、pipを動かすとエラーが

hiroshi-no-MacBook-Air:~ hiroshi$ pip
Traceback (most recent call last):
  File "/usr/local/bin/pip", line 5, in <module>
    from pkg_resources import load_entry_point
  File "build/bdist.macosx-10.9-x86_64/egg/pkg_resources.py", line 2749, in <module>
  File "build/bdist.macosx-10.9-x86_64/egg/pkg_resources.py", line 446, in _build_master
  File "build/bdist.macosx-10.9-x86_64/egg/pkg_resources.py", line 459, in _build_from_requirements
  File "build/bdist.macosx-10.9-x86_64/egg/pkg_resources.py", line 628, in resolve
pkg_resources.DistributionNotFound: pip==1.4.1

setuptoolsを動かすと同じような感じです

hiroshi-no-MacBook-Air:~ hiroshi$ easy_install list
Traceback (most recent call last):
  File "/usr/local/bin/easy_install", line 5, in <module>
    from pkg_resources import load_entry_point
  File "build/bdist.macosx-10.9-x86_64/egg/pkg_resources.py", line 2749, in <module>
  File "build/bdist.macosx-10.9-x86_64/egg/pkg_resources.py", line 446, in _build_master
  File "build/bdist.macosx-10.9-x86_64/egg/pkg_resources.py", line 459, in _build_from_requirements
  File "build/bdist.macosx-10.9-x86_64/egg/pkg_resources.py", line 628, in resolve
pkg_resources.DistributionNotFound: setuptools==1.3.2

はてさて。。。どうしよう。。。brew remove python して再度pythonをinstallし直すのもひとつかなとやってみる(ログは保存し忘れました)

が、removeはできたらしいけどinstallは失敗する。すでにpython 2.7.6_1は存在すると言われる。ナンナのコレ

あるのなら、リンクしなおせば行けるのでは?と思い以下を実行

# linkappsをやると、.appスタイルなアプリを/Applicationsにシンボリックリンクしてくれます。pyhtonIDEとかがlaunchpadの一覧に存在するようになりました。
brew linkapps python
brew link python

コマンド打つたびにターミナルを再起動させながらやったところ、なんとpipとsetuptoolsが動きました。removeはやっぱりうまく行ってなかったのかな。

が、今度はipythonやvirtualenvが動かなくなってorz

次の日(つまり今日)の作業

推測するに、(caskを入れた後, caskが悪いかは不明で多分関係ない)brew update -> upgradeをしたときに、python2.7.6がpython2.7.6_1のバージョン表記に変わったらしい? そのためなのか、/usr/local/binにある一部のpyhtonスクリプトが動かない

一部のPythonスクリプトは例えばipythonやvirtualenvでかなり致命的に困る

/usr/local/bin のスクリプトを見てみると、シェバン指定(#!のこと)がこうなってる

1
2
#!/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/bin/python2.7
# 以下略

pythonが2.7.6_1になった時に、このパスは存在しなくなってまして以下になってます

1
#!/usr/local/Cellar/python/2.7.6_1/Frameworks/Python.framework/Versions/2.7/bin/python2.7

で、このパスを使って検索すると以下のissueに当たりまして

https://github.com/Homebrew/homebrew/issues/28346

Xcodeのアップデートが関係しているのでしょうか・・・確かにAppStoreを見たら4/12にXcode 5.1にアップデートをしています。 でもその前にbrew upgradeはやってたので、あまり関係なさそうですが。

とにかく解消方法は以下になると思われます

  • pip uninstall -> installして該当のライブラリなど入れなおす
  • シェバンを正しいパスに変更する(置き換えする)

homwbrewというかpipで管理してるため、シェバンの書き換えはあまり望ましくないと思われるので、pipでuninstallをしてからinstallすることにして実行

該当しそうなライブラリを探して行った結果、動作するようになりました。

再インストールしたライブラリを探すときは、findとgrepの組み合わせで、シェバンの特定の文字列をキーにリストアップ

find /usr/local/bin/. -name "*" -exec grep -l “Cellar/python/2.7.6/Frameworks" {} \;

自分の場合は ipython , virtualenv(clone, wrapper), pyflakes などがありましたが、これらすべてuninstall -> installした結果、/usr/local/bin/ の中にあるスクリプトが新しいものになっていました。

比較

ここで、動くようになったスクリプトのシェバンが何になっているのかが気になるので比較してみると

https://twitter.com/hrs_sano645/status/462762287426965504

https://twitter.com/hrs_sano645/status/462762358151323648

となりました。brew側が実行パスの位置を /usr/local/opt/ あたりに統一してるような感じです。optの中にあるpythonは /usr/local/Cellar/python/ にシンボリックリンクされてました。

具体的な原因がわからなかったので、なんとも腑に落ちないところになりましたが、とりあえず実用は問題無いとして、終わりとします。

brew doctorで見たpython3.4のシンボリックリンクがおかしい(デッドリン���状���?)話は brew prune を行ったので問題なくなったと思います。(が普段python3は使ってないしpipなども入れてなかったので、後々問題になるかもしれませんが)

スポンサードリンク

About Me

Pythonとガジェットとモノづくりが好きな人の日々の備忘録。

スポンサードリンク