首页 > 解决方案 > 无法在 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 版本和操作系统都是 64 位的,所以这个问题绝对不是第二个“可能的原因”。我也很确定我已经安装了 OpenMP:查找说明,就我而言,我会这样做brew install llvm并且安装成功。我也不确定为什么它似乎找不到/usr/local/lib/python2.7/site-packages/xgboost/./lib/libxgboost.dylib,因为我可以cd进入该目录并看到libxgboost.dylib很好。看起来稍后在它确实找到它的消息中,但仍然抛出错误?这里到底发生了什么,我能做些什么来解决这个错误?

标签: pythonmacospython-importimporterrorxgboost

解决方案


先安装 xgboost:

pip install xgboost

然后使用:

brew install libomp

最后:

import xgboost as

推荐阅读