
我已經使用 pip for Python 2.7.16 成功安裝了 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版本和作業系統都是64位元的,所以這個問題絕對不是第二個「可能的原因」。我也很確定我已經安裝了 OpenMP:查找說明,對於我的情況,我會這樣做brew install llvm
並且安裝成功。我也不確定為什麼它似乎找不到/usr/local/lib/python2.7/site-packages/xgboost/./lib/libxgboost.dylib
,因為我可以cd
進入該目錄並看到libxgboost.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或以上版本。