LoginSignup
3
4

More than 1 year has passed since last update.

【備忘録】LightGBMインポートでエラー

Last updated at Posted at 2021-10-05

結論

LightGBMをインストールする際はとりあえずpipではなく、ちゃんとLightGBMのリポジトリに記載されている手順に従うこと!

エラー内容

環境はMacOS。LightGBMを使いたかったのでpipでインストール。インストールの際は特にエラーはなかった。

pip install lightgbm

しかしimportしようとするとOSError (OSError: dlopen ~ Library not loaded ~ Reason: image not found)が起こってしまった。

#[IN]:
import lightgbm as lgb

エラーは以下のようなかんじ。

dlopen(/Users/*site-packages/lightgbm/lib_lightgbm.so, 6): Library not loaded: */libomp/lib/libomp.dylib Referenced from: /Users/*/opt/anaconda3/lib/python3.6/site-packages/lightgbm/lib_lightgbm.so Reason: image not found

このエラーをネットで検索するとそこそこヒットするため多くの人が遭遇している可能性あり。

解決方法

3ステップで解決。

  1. libomp をインストール
  2. LightGBMをアンインストール
  3. LightGBMを再びインストール

1. libompをインストール

brew install libomp

LightGBMのリポジトリに以下の内容が記載されていた。

For macOS (we provide wheels for 3 newest macOS versions) users:

Starting from version 2.2.1, the library file in distribution wheels is built by the Apple Clang (Xcode_8.3.3 for versions 2.2.1 - 2.3.1, and Xcode_9.4.1 from version 2.3.2) compiler. This means that you don't need to install the gcc compiler anymore. Instead of that you need to install the OpenMP library, which is required for running LightGBM on the system with the Apple Clang compiler. You can install the OpenMP library by the following command: brew install libomp.
For version smaller than 2.2.1 and not smaller than 2.1.2, gcc-8 with OpenMP support must be installed first. Refer to Installation Guide for installation of gcc-8 with OpenMP support.
For version smaller than 2.1.2, gcc-7 with OpenMP is required.

どうやらLightGBMをMacで実行するためにはOpenMPライブラリをインストールする必要があるらしい。ということで書かれている通りにlibompをhomebrewでインストール。

2. 一旦LightGBMをpipでアンインストール

LightGBMをアンインストールします。

pip uninstall lightgbm

3. 再びLightGBMをインストール

libompがインストールされている状態でLightGBMをインストールします。これでエラーは解消しました!

pip uninstall lightgbm

参考

https://cocoinit23.com/mac-lightgbm-image-not-found/
https://aotamasaki.hatenablog.com/entry/2018/08/05/215612

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4