[解決したっぽい]ImportError: No module named pyexpat と出て pyenv install がコケる

~~すいません。まだ未解決です。
過去に解決済みの方いらっしゃいましたらアドバイス頂けますと助かります。~~

なんとか動いたっぽいです。

# 直った方法

これで動きました！
https://github.com/yyuu/pyenv/issues/448#issuecomment-220652296

```
export CFLAGS="-I$(brew --prefix openssl)/include"
export LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix sqlite3)/lib"
export CPPFLAGS="-I$(brew --prefix sqlite3)/include"
```

しかしなぜダメで、どうして直ったのかは分かってないです。。。

# 以下経緯

## pyenv で 3.5.2 をインストールしようとしたらエラーが出た

```
$ pyenv install 3.5.2
Installing Python-3.5.2...

BUILD FAILED (OS X 10.12.1 using python-build 20160602)

Inspect or clean up the working tree at /var/folders/g5/b_nhsv5546g9pf7_k7xynpxw0000gn/T/python-build.20161103191050.41922
Results logged to /var/folders/g5/b_nhsv5546g9pf7_k7xynpxw0000gn/T/python-build.20161103191050.41922.log

Last 10 log lines:
    import pip
  File "/var/folders/g5/b_nhsv5546g9pf7_k7xynpxw0000gn/T/tmp3hlbqezp/pip-8.1.1-py2.py3-none-any.whl/pip/__init__.py", line 14, in <module>
  File "/var/folders/g5/b_nhsv5546g9pf7_k7xynpxw0000gn/T/tmp3hlbqezp/pip-8.1.1-py2.py3-none-any.whl/pip/utils/__init__.py", line 27, in <module>
  File "/var/folders/g5/b_nhsv5546g9pf7_k7xynpxw0000gn/T/tmp3hlbqezp/pip-8.1.1-py2.py3-none-any.whl/pip/_vendor/pkg_resources/__init__.py", line 36, in <module>
  File "/private/var/folders/g5/b_nhsv5546g9pf7_k7xynpxw0000gn/T/python-build.20161103191050.41922/Python-3.5.2/Lib/plistlib.py", line 65, in <module>
    from xml.parsers.expat import ParserCreate
  File "/private/var/folders/g5/b_nhsv5546g9pf7_k7xynpxw0000gn/T/python-build.20161103191050.41922/Python-3.5.2/Lib/xml/parsers/expat.py", line 4, in <module>
    from pyexpat import *
ImportError: No module named 'pyexpat'
make: *** [install] Error 1
```

github でも「それ壊れてるから( ´ー｀)ｼﾗﾈｰﾖ」って言われてた

[ImportError: No module named pyexpat · Issue \#544 · yyuu/pyenv](https://github.com/yyuu/pyenv/issues/544)


# homebrew 経由でインストールしたpyenvがおかしくなってしまったのではないか？

一旦なかった事にしたい。

## PYENV_ROOTは消そう

```
$ echo $PYENV_ROOT
/usr/local/var/pyenv
$ rm -r /usr/local/var/pyenv
```

## とりあえずbrew経由で入れた関連は消してみる

```
$ brew uninstall pyenv python
```

## もう1回pyenvをインストールする

```
$ brew install pyenv
```

## 3.5.2 インストールを再チャレンジ

```
$ pyenv install 3.5.2
```

いや、ダメだし！

```

Downloading Python-3.5.2.tar.xz...
-> https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tar.xz
Installing Python-3.5.2...

BUILD FAILED (OS X 10.12.1 using python-build 20160602)

Inspect or clean up the working tree at /var/folders/g5/b_nhsv5546g9pf7_k7xynpxw0000gn/T/python-build.20161103193542.66147
Results logged to /var/folders/g5/b_nhsv5546g9pf7_k7xynpxw0000gn/T/python-build.20161103193542.66147.log

Last 10 log lines:
    import pip
  File "/var/folders/g5/b_nhsv5546g9pf7_k7xynpxw0000gn/T/tmpsbqryzv7/pip-8.1.1-py2.py3-none-any.whl/pip/__init__.py", line 14, in <module>
  File "/var/folders/g5/b_nhsv5546g9pf7_k7xynpxw0000gn/T/tmpsbqryzv7/pip-8.1.1-py2.py3-none-any.whl/pip/utils/__init__.py", line 27, in <module>
  File "/var/folders/g5/b_nhsv5546g9pf7_k7xynpxw0000gn/T/tmpsbqryzv7/pip-8.1.1-py2.py3-none-any.whl/pip/_vendor/pkg_resources/__init__.py", line 36, in <module>
  File "/private/var/folders/g5/b_nhsv5546g9pf7_k7xynpxw0000gn/T/python-build.20161103193542.66147/Python-3.5.2/Lib/plistlib.py", line 65, in <module>
    from xml.parsers.expat import ParserCreate
  File "/private/var/folders/g5/b_nhsv5546g9pf7_k7xynpxw0000gn/T/python-build.20161103193542.66147/Python-3.5.2/Lib/xml/parsers/expat.py", line 4, in <module>
    from pyexpat import *
ImportError: No module named 'pyexpat'
make: *** [install] Error 1
```


# CFLAGS とやらを設定して再チャレンジ

ここを参考に・・・

[Python 3\.4\.2 doesn't build on OS X 10\.10 Yosemite · Issue \#298 · yyuu/pyenv](https://github.com/yyuu/pyenv/issues/298)

```
$ CFLAGS="-I$(xcrun --show-sdk-path)/usr/include" pyenv install 3.5.2
Downloading Python-3.5.2.tar.xz...
-> https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tar.xz
Installing Python-3.5.2...

BUILD FAILED (OS X 10.12.1 using python-build 20160602)

Inspect or clean up the working tree at /var/folders/g5/b_nhsv5546g9pf7_k7xynpxw0000gn/T/python-build.20161103193925.80736
Results logged to /var/folders/g5/b_nhsv5546g9pf7_k7xynpxw0000gn/T/python-build.20161103193925.80736.log

Last 10 log lines:
    import pip
  File "/var/folders/g5/b_nhsv5546g9pf7_k7xynpxw0000gn/T/tmp7cp6pxz_/pip-8.1.1-py2.py3-none-any.whl/pip/__init__.py", line 14, in <module>
  File "/var/folders/g5/b_nhsv5546g9pf7_k7xynpxw0000gn/T/tmp7cp6pxz_/pip-8.1.1-py2.py3-none-any.whl/pip/utils/__init__.py", line 27, in <module>
  File "/var/folders/g5/b_nhsv5546g9pf7_k7xynpxw0000gn/T/tmp7cp6pxz_/pip-8.1.1-py2.py3-none-any.whl/pip/_vendor/pkg_resources/__init__.py", line 36, in <module>
  File "/private/var/folders/g5/b_nhsv5546g9pf7_k7xynpxw0000gn/T/python-build.20161103193925.80736/Python-3.5.2/Lib/plistlib.py", line 65, in <module>
    from xml.parsers.expat import ParserCreate
  File "/private/var/folders/g5/b_nhsv5546g9pf7_k7xynpxw0000gn/T/python-build.20161103193925.80736/Python-3.5.2/Lib/xml/parsers/expat.py", line 4, in <module>
    from pyexpat import *
ImportError: No module named 'pyexpat'
make: *** [install] Error 1
```

変わらないか…

# もう何か2系もコケる

```
 $ pyenv install 2.7.12
Downloading Python-2.7.12.tar.xz...
-> https://www.python.org/ftp/python/2.7.12/Python-2.7.12.tar.xz
Installing Python-2.7.12...
WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
Installing pip from https://bootstrap.pypa.io/get-pip.py...
error: failed to install pip via get-pip.py

BUILD FAILED (OS X 10.12.1 using python-build 20160602)

Inspect or clean up the working tree at /var/folders/g5/b_nhsv5546g9pf7_k7xynpxw0000gn/T/python-build.20161103194730.10697
Results logged to /var/folders/g5/b_nhsv5546g9pf7_k7xynpxw0000gn/T/python-build.20161103194730.10697.log

Last 10 log lines:
    info = _mac_ver_xml()
  File "/usr/local/var/pyenv/versions/2.7.12/lib/python2.7/platform.py", line 739, in _mac_ver_xml
    pl = plistlib.readPlist(fn)
  File "/usr/local/var/pyenv/versions/2.7.12/lib/python2.7/plistlib.py", line 78, in readPlist
    rootObject = p.parse(pathOrFile)
  File "/usr/local/var/pyenv/versions/2.7.12/lib/python2.7/plistlib.py", line 401, in parse
    from xml.parsers.expat import ParserCreate
  File "/usr/local/var/pyenv/versions/2.7.12/lib/python2.7/xml/parsers/expat.py", line 4, in <module>
    from pyexpat import *
ImportError: No module named pyexpat
```