Mac下安装lightgbm完美方案(fatal error: _stdio.h: No such file or directory)

1. 安装环境

系统:macOS High Sierra 10.13.4
Xcode:Version 9.1

$ clang -v
Apple LLVM version 9.0.0 (clang-900.0.38)
Target: x86_64-apple-darwin17.5.0
Thread model: posix

2. 错误安装

直接使用命令:

pip install lightgbm

或者直接在pycharm里安装lightgbm,版本都是2.1.1

在执行:

import lightgbm as lgb

时会报下列错误信息:

OSError: dlopen(/Usrs/chao/.pyenv/versions/3.6.2/lib/python3.6/site-packages/lightgbm/lib_lightgbm.so, 6): Library not loaded: /Usrs/chao/.../libgomp.1.dylib
Referenced from: /Usrs/chao/.pyenv/versions/3.6.2/lib/python3.6/site-packages/lightgbm/lib_lightgbm.so
Reason: image not found

那么请先执行:

pip uninstall lightgbm

3. 正确安装

1. 安装homebrew

2. 安装依赖

依次执行以下命令:

brew install cmake
brew install gcc@7 --without-multilib
git clone --recursive https://github.com/Microsoft/LightGBM ; cd LightGBM
mkdir build ; cd build

3. 编译

cmake -DCMAKE_CXX_COMPILER=g++-7 -DCMAKE_C_COMPILER=gcc-7 ..  

输出信息:

-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
// ...
-- Found OpenMP: TRUE   
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/chao/Documents/python/LightGBM/build

4. 构建

make -j

如果不报错,则输出信息:

Scanning dependencies of target lightgbm
Scanning dependencies of target _lightgbm
[  1%] Building CXX object CMakeFiles/_lightgbm.dir/src/io/dataset.cpp.o
[  3%] Building CXX object CMakeFiles/lightgbm.dir/src/io/dataset.cpp.o
// ...
[ 90%] Building CXX object CMakeFiles/lightgbm.dir/src/treelearner/tree_learner.cpp.o
[ 91%] Linking CXX executable ../lightgbm
[ 95%] Built target lightgbm
[ 96%] Linking CXX shared library ../lib_lightgbm.so
[100%] Built target _lightgbm

并在上级目录LightGBM下生成lib_lightgbm.so和lightgbm两个文件。

cd ..
ls

【跳到第5步去】

如果报错(重点来了,全网只此一家!),一堆错其实都是同一个:

/usr/local/Cellar/gcc@7/7.3.0/lib/gcc/7/gcc/x86_64-apple-darwin17.5.0/7.3.0/include-fixed/stdio.h:78:10: fatal error: _stdio.h: No such file or directory
 #include <_stdio.h>
          ^~~~~~~~~~
compilation terminated.

则在Finder里按名称匹配搜索_stdio.h,找到以

Copyright (c) 2000, 2005, 2007, 2009, 2010 Apple Inc. All rights reserved.

注释信息开头的_stdio.h,复制到以下目录(具体地址参见报错信息):

/usr/local/Cellar/gcc@7/7.3.0/lib/gcc/7/gcc/x86_64-apple-darwin17.5.0/7.3.0/include-fixed/

再重新执行第4步,然后项目构建成功。

5. 安装

继续执行命令:

cd python-package
pip install lightgbm

此时已经安装成功,但只限于终端上使用,测试一下:

$ python
>>> import lightgbm as lgb
>>> lgb.LGBMClassifier()
LGBMClassifier(boosting_type='gbdt', class_weight=None, colsample_bytree=1.0,
        learning_rate=0.1, max_depth=-1, min_child_samples=20,
        min_child_weight=0.001, min_split_gain=0.0, n_estimators=100,
        n_jobs=-1, num_leaves=31, objective=None, random_state=None,
        reg_alpha=0.0, reg_lambda=0.0, silent=True, subsample=1.0,
        subsample_for_bin=200000, subsample_freq=1)

终端使用OK,但是pycharm里还是不行,不出意外会显示:

ImportError: No module named "lightgbm"

那么请继续配置。

6. 手动添加python包

拷贝/LightGBM/lib_lightgbm.so文件到/LightGBM/python-package/lightgbm/目录下,再将/LightGBM/python-package/lightgbm整个文件夹拷贝到系统python的包目录下,如:

/Usrs/chao/.pyenv/versions/3.6.2/lib/python3.6/site-packages/

End.

  • 8
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值