Python で xgboost をインポートできません

Python で xgboost をインポートできません

Python 2.7.16 の pip を使用して xgboost を正常にインストールしました (macOS High Sierra で Homebrew を使用してこの Python バージョンをインストールしました)。問題は、以下のエラー メッセージのように、Python で xgboost をインポートできないことです。

mac-128644:~ user$ python
Python 2.7.16 (default, Apr 12 2019, 15:32:52) 
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import xgboost
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/xgboost/__init__.py", line 11, in <module>
    from .core import DMatrix, Booster
  File "/usr/local/lib/python2.7/site-packages/xgboost/core.py", line 163, in <module>
    _LIB = _load_lib()
  File "/usr/local/lib/python2.7/site-packages/xgboost/core.py", line 154, in _load_lib
    'Error message(s): {}\n'.format(os_error_list))
xgboost.core.XGBoostError: XGBoost Library (libxgboost.dylib) could not be loaded.
Likely causes:
  * OpenMP runtime is not installed (vcomp140.dll or libgomp-1.dll for Windows, libgomp.so for UNIX-like OSes)
  * You are running 32-bit Python on a 64-bit OS
Error message(s): ['dlopen(/usr/local/lib/python2.7/site-packages/xgboost/./lib/libxgboost.dylib, 6): Library not loaded: /usr/local/opt/gcc/lib/gcc/8/libgomp.1.dylib\n  Referenced from: /usr/local/lib/python2.7/site-packages/xgboost/./lib/libxgboost.dylib\n  Reason: no suitable image found.  Did find:\n\t/usr/local/lib/python2.7/site-packages/xgboost/./lib/libxgboost.dylib/libgomp.1.dylib: stat() failed with errno=20']

私はPythonのバージョンとOSの両方が64ビットであることを確認したので、問題は間違いなく2番目の「考えられる原因」ではありません。また、OpenMPがインストールされていることも確かです。手順を調べてみると、私の場合はそうしbrew install llvm、インストールは成功しました。また、なぜ見つからないのかわかりません。そのディレクトリに入って問題なく見ることが/usr/local/lib/python2.7/site-packages/xgboost/./lib/libxgboost.dylibできるのに。メッセージの後半で、cdlibxgboost.dylibした見つけましたが、まだエラーが発生します。ここでは正確に何が起こっているのでしょうか、そしてこのエラーを修正するにはどうすればよいでしょうか?

答え1

gcc をインストールしましたか? 次の手順を試してください。

brew install gcc@8 それから

brew switch gcc 8.3.0_2またはインストールされたバージョン

brew link --overwrite gccこれで私の問題は解決したようです。

答え2

PythonインタープリターはPython 2.7だと表示していますが、pypi xgboost プロジェクトPython 3.8 以上が必要です。

関連情報