LoginSignup
3
2

More than 5 years have passed since last update.

MacでTensorFlowを使いたい場合はpipenvはbrewでなくてpipでinstallするのがオススメ

Last updated at Posted at 2018-11-11

概要

皆さんおなじみMacのパッケージ管理ツールであるbrewを用いて、Pythonのパッケージ管理フレームワークpipenvもinstallすることができます。公式サイトでもbrewでinstallするように書いてます。

ですが。pipenvをbrewでinstallしているとpipenv installコマンドを実行した際にエラーが起きる場合があります。なので、pipenvはbrewでなくてpipでinstallする方がオススメです。

発生条件

  • Python3をbrewでinstallしており、最新の3.7.0がinstallされている(2018年11月現在)
  • 何らかの理由で3.6系を使いたくなり、Python3を最新の3.7.0でなく、3.6.5_1に落とす

※私の場合の理由:TensorFlowを使いたかったから

環境

macOS High Sierra
バージョン 10.13.6
Homebrew 1.8.1
brewでinstallしたPython 3.7.0 

発生するエラー

英語ですがこの投稿と同じ現象が発生しました。やりたいこともこの投稿とほぼ同じでした。

$ brew switch python 3.6.5_1 
Cleaning /usr/local/Cellar/python/3.7.0
Cleaning /usr/local/Cellar/python/3.6.5_1
Cleaning /usr/local/Cellar/python/3.6.5
25 links created for /usr/local/Cellar/python/3.6.5_1
$ pipenv install --python 3.6
dyld: Library not loaded: @executable_path/../.Python
  Referenced from: /usr/local/Cellar/pipenv/2018.7.1/libexec/bin/python3.7
  Reason: image not found
Abort trap: 6

Pythonの3.6系を使いたい理由

TensorFlowを使おうとしたら、現在のTensorFlowはまだPython3.7に対応していないようでしたので、3.6系に落としました。

https://github.com/tensorflow/tensorflow/issues/17022
https://github.com/pypa/pipenv/issues/2619

brewでinstallしたPython3のバージョンの落とし方

この記事を参考にしました。Python 3.7.0が入っている前提です。
https://apple.stackexchange.com/questions/329187/homebrew-rollback-from-python-3-7-to-python-3-6-5-x

$ brew unlink python
$ brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb
$ brew switch python 3.6.5_1

brewでinstallしたpipenvをuninstall

使わないので消しちゃいます。アディオス!

$ brew uninstall pipenv

Python 3.6.5のpipを用いてpipenvをinstall

pipコマンドでpipenvを入れ直します。

$ python3 --version
Python 3.6.5
$ pip3 install pipenv
Collecting pipenv
  Using cached https://files.pythonhosted.org/packages/90/06/0008f53835495fbbf6e31ced9119b8f517e1271bdefcf0d04aaa9f28dbf4/pipenv-2018.10.13-py3-none-any.whl
Requirement already satisfied: certifi in /usr/local/lib/python3.6/site-packages (from pipenv) (2018.10.15)
Requirement already satisfied: virtualenv-clone>=0.2.5 in /usr/local/lib/python3.6/site-packages (from pipenv) (0.4.0)
Requirement already satisfied: pip>=9.0.1 in /usr/local/lib/python3.6/site-packages (from pipenv) (18.1)
Requirement already satisfied: setuptools>=36.2.1 in /usr/local/lib/python3.6/site-packages (from pipenv) (39.2.0)
Requirement already satisfied: virtualenv in /usr/local/lib/python3.6/site-packages (from pipenv) (16.1.0)
Installing collected packages: pipenv
Successfully installed pipenv-2018.10.13
$ pip3 list
Package          Version
---------------- ----------
certifi          2018.10.15
pip              18.1
pipenv           2018.10.13
setuptools       39.2.0
virtualenv       16.1.0
virtualenv-clone 0.4.0
wheel            0.31.1

pipenvを用いてTensorFlowをinstall

さて、ここが本題。無事installできるかどうか。。

$ cd /path/to/your/project
$ pipenv shell
(project name) $ pipenv install tensorflow==1.9.0
Installing tensorflow==1.9.0…
Collecting tensorflow==1.9.0
  Using cached https://files.pythonhosted.org/packages/57/38/96ee343729363297d62311272adc28b466d30f74c87dc645badb0102af56/tensorflow-1.9.0-cp36-cp36m-macosx_10_11_x86_64.whl
(略)
Adding tensorflow to Pipfile's [packages]…
Pipfile.lock (31dec2) out of date, updating to (ca72e7)…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…

できた!Sweet!!

雑感

半日ぐらいハマりました。。終わってしまえば、他の方に記事に書いてあるやんって感じですが。

https://qiita.com/tanuk1647/items/cad362af79f528a5c348
https://narito.ninja/detail/58/

どなたかのお役に立てば幸いです!
久々に書いたのでなんかスッキリ。

3
2
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
3
2