Macでpip install pandasしたらエラーが出たので対処した

作業履歴
========

```sh
$ pip install pandas
Downloading/unpacking pandas
  Downloading pandas-0.15.2-cp27-none-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (6.7MB): 6.7MB downloaded
Downloading/unpacking numpy>=1.7.0 (from pandas)
  Downloading numpy-1.9.1-cp27-none-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (3.6MB): 3.6MB downloaded
Requirement already satisfied (use --upgrade to upgrade): python-dateutil in /usr/local/lib/python2.7/site-packages (from pandas)
Downloading/unpacking pytz>=2011k (from pandas)
  Downloading pytz-2014.10-py2.py3-none-any.whl (477kB): 477kB downloaded
Requirement already satisfied (use --upgrade to upgrade): six in /usr/local/lib/python2.7/site-packages (from python-dateutil->pandas)
Installing collected packages: pandas, numpy, pytz
Cleaning up...
Exception:
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.5.6-py2.7.egg/pip/basecommand.py", line 122, in main
    status = self.run(options, args)
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.5.6-py2.7.egg/pip/commands/install.py", line 283, in run
    requirement_set.install(install_options, global_options, root=options.root_path)
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.5.6-py2.7.egg/pip/req.py", line 1435, in install
    requirement.install(install_options, global_options, *args, **kwargs)
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.5.6-py2.7.egg/pip/req.py", line 671, in install
    self.move_wheel_files(self.source_dir, root=root)
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.5.6-py2.7.egg/pip/req.py", line 901, in move_wheel_files
    pycompile=self.pycompile,
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.5.6-py2.7.egg/pip/wheel.py", line 215, in move_wheel_files
    clobber(source, lib_dir, True)
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.5.6-py2.7.egg/pip/wheel.py", line 209, in clobber
    shutil.copy2(srcfile, destfile)
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 130, in copy2
    copyfile(src, dst)
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 83, in copyfile
    with open(dst, 'wb') as fdst:
IOError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/site-packages/numpy/__config__.pyc'

Storing debug log for failure in /Users/tmsanrinsha/.pip/pip.log
```

`IOError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/site-packages/numpy/__config__.pyc'`とあることに今気づいたのでnumpyのインストールの仕方がまずかったらしい。

しかし、作業中はわからなかったのでとりあえず[fonnesbeck/ScipySuperpack @ GitHub](http://fonnesbeck.github.io/ScipySuperpack/)のスクリプトを実行すればうまくいくかなと思い実行。

```sh
$ curl 'https://raw.githubusercontent.com/fonnesbeck/ScipySuperpack/master/install_superpack.sh' | sh
```

これも今気づいたが途中に`stty: stdin isn't a terminal`という文言があり、その後の選択

```
Would you like to use Python 2.7 or Python 3.4? (2/3)
```

で選択できずに

```
Invalid selection. Quitting.
```

となり失敗した。なのでサイトに書いてあるように

```sh
$ curl -o install_superpack.sh https://raw.githubusercontent.com/fonnesbeck/ScipySuperpack/master/install_superpack.sh
$ sh install_superpack.sh
```

する必要があるようだ。でも、今試したところ、色々入るのでこれはやめたほうがいいかも。

で、なんとなくnumpyが悪いのかと思い、`pip install numpy`すると`pip install pandas`と同じエラーが出たので、pipが怪しいと思い、

```sh
$ pip install -U pip
```

した後、

```
$ pip install -U numpy
```

すると、エラーが変わって、

```
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.10-x86_64/egg/pkg_resources.py", line 2951, in <module>
  File "build/bdist.macosx-10.10-x86_64/egg/pkg_resources.py", line 563, in _build_master
  File "build/bdist.macosx-10.10-x86_64/egg/pkg_resources.py", line 576, in _build_from_requirements
  File "build/bdist.macosx-10.10-x86_64/egg/pkg_resources.py", line 755, in resolve
pkg_resources.DistributionNotFound: pip==1.5.6
```

となった。これは後にわかることだが、pipをupdateしたために出たエラー。

pythonが悪いのかと思い

```
$ brew uninstall python
$ brew install python --build-fromsource
```

したが、かわらず。

```
$ brew uninstall numpy
$ brew install numpy
```

すると、

```
Warning: It looks like the deprecated gfortran formula is installed.
This causes build problems with numpy. gfortran is now provided by
the gcc formula. Please run:
    brew rm gfortran
    brew install gcc
```

こんなメッセージが出たので、

```
$ brew rm gfortran
$ brew install gcc
$ brew install numpy
```

すると、

```
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink lib/python2.7/site-packages/numpy/__config__.py
Target /usr/local/lib/python2.7/site-packages/numpy/__config__.py
already exists. You may want to remove it:
  rm '/usr/local/lib/python2.7/site-packages/numpy/__config__.py'

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

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

Possible conflicting files are:
/usr/local/lib/python2.7/site-packages/numpy/__config__.py
/usr/local/lib/python2.7/site-packages/numpy/__init__.py
```

というエラーが出てきた。これが`pip install pandas`で出てきたエラーの原因っぽい。

```
$ brew link --overwrite numpy
```

して解決する。

その後、pipのエラーについて、[Python - Homebrew で pip が DistributionError を出す場合の対処 - Qiita](http://qiita.com/methane/items/526cc5c711623d4aeeb9)という記事を見つけたので、そこに書いてあるように

```
$ rm /usr/local/bin/pip /usr/local/bin/pip2 /usr/local/bin/pip2.7
$ python -m pip install --upgrade --force-reinstall pip
```

すると、解決して、`pip install pandas`できた。

まとめ
======

結局最初の`pip install pandas`で出たエラーは

```
IOError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/site-packages/numpy/__config__.pyc'
```

とあるので、numpyの問題。これを解決するにはnumpyをアンインストールして、インストールすればよい。ただし、エラーが出たのでそれに従う。

```sh
$ brew uninstall numpy
$ brew rm gfortran
$ brew install gcc
$ brew install numpy
$ brew link --overwrite numpy
```

また、途中pipをアップデートしてしまったため出てしまったエラーは

```sh
$ rm /usr/local/bin/pip /usr/local/bin/pip2 /usr/local/bin/pip2.7
$ python -m pip install --upgrade --force-reinstall pip
```

とすることで解決する。