Python3.8でscikit-learnなどのライブラリがインストールできない問題

# 概要
以下のサイトよりGUI上から最新のPython3.8をインストールしましたが、
機械学習を行う際に使用する基本的なライブラリ(scikit-learnなど)
をインストールできなかったので、
その解決方法についてお伝えします。

結論からいうと、**Python3.7.3を再インストールしたら上手く行きました。**


[Pythonサイト](https://www.python.jp/install/macos/index.html)

# エラー内容
```
% pip3 install scikit-learn

・・・中略

Building wheels for collected packages: scikit-learn
Building wheel for scikit-learn (setup.py) ... error
ERROR: Command errored out with exit status 1:
command: /Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/v2/5h5jxmpx4vbdc_301kmd1cc00000gn/T/pip-install-hnm9nz72/scikit-learn/setup.py'"'"'; __file__='"'"'/private/var/folders/v2/5h5jxmpx4vbdc_301kmd1cc00000gn/T/pip-install-hnm9nz72/scikit-learn/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /private/var/folders/v2/5h5jxmpx4vbdc_301kmd1cc00000gn/T/pip-wheel-dptnltmf --python-tag cp38
cwd: /private/var/folders/v2/5h5jxmpx4vbdc_301kmd1cc00000gn/T/pip-install-hnm9nz72/scikit-learn/
Complete output (50 lines):
Partial import of sklearn during the build process.
C compiler: gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -arch x86_64 -g

compile options: '-c'
extra options: '-fopenmp'
gcc: test_openmp.c
clang: error: unsupported option '-fopenmp'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/private/var/folders/v2/5h5jxmpx4vbdc_301kmd1cc00000gn/T/pip-install-hnm9nz72/scikit-learn/setup.py", line 290, in <module>
setup_package()
File "/private/var/folders/v2/5h5jxmpx4vbdc_301kmd1cc00000gn/T/pip-install-hnm9nz72/scikit-learn/setup.py", line 286, in setup_package
setup(**metadata)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/numpy/distutils/core.py", line 137, in setup
config = configuration()
File "/private/var/folders/v2/5h5jxmpx4vbdc_301kmd1cc00000gn/T/pip-install-hnm9nz72/scikit-learn/setup.py", line 174, in configuration
config.add_subpackage('sklearn')
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/numpy/distutils/misc_util.py", line 1033, in add_subpackage
config_list = self.get_subpackage(subpackage_name, subpackage_path,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/numpy/distutils/misc_util.py", line 999, in get_subpackage
config = self._get_configuration_from_setup_py(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/numpy/distutils/misc_util.py", line 941, in _get_configuration_from_setup_py
config = setup_module.configuration(*args)
File "sklearn/setup.py", line 76, in configuration
maybe_cythonize_extensions(top_path, config)
File "/private/var/folders/v2/5h5jxmpx4vbdc_301kmd1cc00000gn/T/pip-install-hnm9nz72/scikit-learn/sklearn/_build_utils/__init__.py", line 42, in maybe_cythonize_extensions
with_openmp = check_openmp_support()
File "/private/var/folders/v2/5h5jxmpx4vbdc_301kmd1cc00000gn/T/pip-install-hnm9nz72/scikit-learn/sklearn/_build_utils/openmp_helpers.py", line 140, in check_openmp_support
raise CompileError(err_message)
distutils.errors.CompileError:
***

It seems that scikit-learn cannot be built with OpenMP support.

- Make sure you have followed the installation instructions:

https://scikit-learn.org/dev/developers/advanced_installation.html

- If your compiler supports OpenMP but the build still fails, please
submit a bug report at:

https://github.com/scikit-learn/scikit-learn/issues

- If you want to build scikit-learn without OpenMP support, you can set
the environment variable SKLEARN_NO_OPENMP and rerun the build
command. Note however that some estimators will run in sequential
mode and their `n_jobs` parameter will have no effect anymore.

***

----------------------------------------
ERROR: Failed building wheel for scikit-learn
Running setup.py clean for scikit-learn
Failed to build scikit-learn
Installing collected packages: scikit-learn
Running setup.py install for scikit-learn ... error
ERROR: Command errored out with exit status 1:
command: /Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/v2/5h5jxmpx4vbdc_301kmd1cc00000gn/T/pip-install-hnm9nz72/scikit-learn/setup.py'"'"'; __file__='"'"'/private/var/folders/v2/5h5jxmpx4vbdc_301kmd1cc00000gn/T/pip-install-hnm9nz72/scikit-learn/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/v2/5h5jxmpx4vbdc_301kmd1cc00000gn/T/pip-record-s68c90r2/install-record.txt --single-version-externally-managed --compile
cwd: /private/var/folders/v2/5h5jxmpx4vbdc_301kmd1cc00000gn/T/pip-install-hnm9nz72/scikit-learn/
Complete output (50 lines):
Partial import of sklearn during the build process.
C compiler: gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -arch x86_64 -g

compile options: '-c'
extra options: '-fopenmp'
gcc: test_openmp.c
clang: error: unsupported option '-fopenmp'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/private/var/folders/v2/5h5jxmpx4vbdc_301kmd1cc00000gn/T/pip-install-hnm9nz72/scikit-learn/setup.py", line 290, in <module>
setup_package()
File "/private/var/folders/v2/5h5jxmpx4vbdc_301kmd1cc00000gn/T/pip-install-hnm9nz72/scikit-learn/setup.py", line 286, in setup_package
setup(**metadata)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/numpy/distutils/core.py", line 137, in setup
config = configuration()
File "/private/var/folders/v2/5h5jxmpx4vbdc_301kmd1cc00000gn/T/pip-install-hnm9nz72/scikit-learn/setup.py", line 174, in configuration
config.add_subpackage('sklearn')
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/numpy/distutils/misc_util.py", line 1033, in add_subpackage
config_list = self.get_subpackage(subpackage_name, subpackage_path,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/numpy/distutils/misc_util.py", line 999, in get_subpackage
config = self._get_configuration_from_setup_py(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/numpy/distutils/misc_util.py", line 941, in _get_configuration_from_setup_py
config = setup_module.configuration(*args)
File "sklearn/setup.py", line 76, in configuration
maybe_cythonize_extensions(top_path, config)
File "/private/var/folders/v2/5h5jxmpx4vbdc_301kmd1cc00000gn/T/pip-install-hnm9nz72/scikit-learn/sklearn/_build_utils/__init__.py", line 42, in maybe_cythonize_extensions
with_openmp = check_openmp_support()
File "/private/var/folders/v2/5h5jxmpx4vbdc_301kmd1cc00000gn/T/pip-install-hnm9nz72/scikit-learn/sklearn/_build_utils/openmp_helpers.py", line 140, in check_openmp_support
raise CompileError(err_message)
distutils.errors.CompileError:
***

It seems that scikit-learn cannot be built with OpenMP support.

- Make sure you have followed the installation instructions:

https://scikit-learn.org/dev/developers/advanced_installation.html

- If your compiler supports OpenMP but the build still fails, please
submit a bug report at:

https://github.com/scikit-learn/scikit-learn/issues

- If you want to build scikit-learn without OpenMP support, you can set
the environment variable SKLEARN_NO_OPENMP and rerun the build
command. Note however that some estimators will run in sequential
mode and their `n_jobs` parameter will have no effect anymore.

***

----------------------------------------
ERROR: Command errored out with exit status 1: /Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/v2/5h5jxmpx4vbdc_301kmd1cc00000gn/T/pip-install-hnm9nz72/scikit-learn/setup.py'"'"'; __file__='"'"'/private/var/folders/v2/5h5jxmpx4vbdc_301kmd1cc00000gn/T/pip-install-hnm9nz72/scikit-learn/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/v2/5h5jxmpx4vbdc_301kmd1cc00000gn/T/pip-record-s68c90r2/install-record.txt --single-version-externally-managed --compile Check the logs for full command output.
```
WheelというPythonのパッケージの形式（フォーマット）が上手く入っていないという書いてあるようにも見えますが、とにかく何やっても上手く行きませんでした。

#解決プロセス
`pyenv`で`Python`のバージョンを`3.7.3`に落として再度試すことにしました。

```
% pyenv install --list
```
を押下すると利用可能なバージョンが出てきます。

```
Available versions:
 ・・・
  3.6.8
  3.7.0
  3.7-dev
  3.7.1
  3.7.2
  3.7.3
  ・・・
```
これを最新版（`Python3.7.3`）に変更します。

```
% pyenv install 3.7.3

python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.7.3.tar.xz...
-> https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xz
Installing Python-3.7.3...
python-build: use readline from homebrew

BUILD FAILED (OS X 10.15.1 using python-build 20180424)

Inspect or clean up the working tree at /var/folders/v2/5h5jxmpx4vbdc_301kmd1cc00000gn/T/python-build.20191106073450.19950
Results logged to /var/folders/v2/5h5jxmpx4vbdc_301kmd1cc00000gn/T/python-build.20191106073450.19950.log

Last 10 log lines:
  File "/private/var/folders/v2/5h5jxmpx4vbdc_301kmd1cc00000gn/T/python-build.20191106073450.19950/Python-3.7.3/Lib/ensurepip/__main__.py", line 5, in <module>
    sys.exit(ensurepip._main())
  File "/private/var/folders/v2/5h5jxmpx4vbdc_301kmd1cc00000gn/T/python-build.20191106073450.19950/Python-3.7.3/Lib/ensurepip/__init__.py", line 204, in _main
    default_pip=args.default_pip,
  File "/private/var/folders/v2/5h5jxmpx4vbdc_301kmd1cc00000gn/T/python-build.20191106073450.19950/Python-3.7.3/Lib/ensurepip/__init__.py", line 117, in _bootstrap
    return _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
  File "/private/var/folders/v2/5h5jxmpx4vbdc_301kmd1cc00000gn/T/python-build.20191106073450.19950/Python-3.7.3/Lib/ensurepip/__init__.py", line 27, in _run_pip
    import pip._internal
zipimport.ZipImportError: can't decompress data; zlib not available
make: *** [install] Error 1
```
見事にここでも詰まりました。

```
% pyenv versions

  system
* 3.6.6 (set by /Users/fez-pc-006/.pyenv/version)
```
`3.6.6`には対応しているみたいだったのですが、できる限り最新に近いものを使用したかったのでもう少し探ることにしました。

色々と調べて結論インストールできたやり方がこちら、

```
#CFLAGS="-I$(xcrun --show-sdk-path)/usr/include" pyenv install -v 3.7.3
```
この後、使用するバージョンを確定させます。

```
# python --version

Python 2.7.16
```
現在のversionが`2.7.16`になっているので`3.7.3`に変更します。

```
# pyenv global 3.7.3
```
これで変わらない時は以下を試してみてください。

```
# which python

/usr/bin/python
```
ここで指しているpythonのディレクトリが`pyenv`ではない場合は、以下を試してみてください。

```
# vim /etc/paths

/usr/local/bin/
/usr/bin/
/bin
/usr/sbin
/sbin
```
`/usr/local/bin/`の配下に`pypenv`があるので一番上に来るように
編集すると、最初のディレクトリの中にある`Python`ファイルを見に行くようになります。

これで再度試してみてダメな場合は以下のコマンドを叩いてください。

```
# eval "$(pyenv init -)"

# which python
/var/root/.pyenv/shims/python
```
これでようやくファイルの向き先が変わりました。

```
# python --version
Python 3.7.3
```

これで`pip`コマンドを使って`scikit-learn`をインストールすれば上手く行くと思います。