Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lightgbm OSError, Library not loaded

If I simply do:

import lightgbm as lgb

I'm getting

python script.py 
Traceback (most recent call last):
File "script.py", line 4, in <module>
import lightgbm as lgb
File "/usr/local/lib/python2.7/site-packages/lightgbm/__init__.py", line 8, in <module>
from .basic import Booster, Dataset
File "/usr/local/lib/python2.7/site-packages/lightgbm/basic.py", line 31, in <module>
_LIB = _load_lib()
File "/usr/local/lib/python2.7/site-packages/lightgbm/basic.py", line 26, in _load_lib
lib = ctypes.cdll.LoadLibrary(lib_path[0])
File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 440, in LoadLibrary
return self._dlltype(name)
File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 362, in __init__
self._handle = _dlopen(self._name, mode)
OSError: dlopen(/usr/local/lib/python2.7/site-packages/lightgbm/lib_lightgbm.so, 6): Library not loaded: /usr/local/opt/gcc/lib/gcc/7/libgomp.1.dylib
Referenced from: /usr/local/lib/python2.7/site-packages/lightgbm/lib_lightgbm.so
Reason: image not found

I seem to have everything installed correct:

python -m pip install lightgbm
Requirement already satisfied: lightgbm in /usr/local/lib/python2.7/site-packages
Requirement already satisfied: wheel in /usr/local/lib/python2.7/site-packages (from lightgbm)
Requirement already satisfied: scikit-learn in /usr/local/lib/python2.7/site-packages (from lightgbm)
Requirement already satisfied: scipy in /usr/local/lib/python2.7/site-packages (from lightgbm)
Requirement already satisfied: numpy in /usr/local/lib/python2.7/site-packages (from lightgbm)

I do have a 6 folder in my GCC folder. Should I need to install another version of GCC for this to work?

enter image description here

like image 694
LampShade Avatar asked Jul 05 '17 23:07

LampShade


7 Answers

All the above answers didn't work for me. On Mac, if I installed the libomp using brew fixed the problem: Refer: link

brew install libomp
like image 59
ShellZero Avatar answered Oct 07 '22 07:10

ShellZero


I find a similar problem here LightGBM
The answer and comment may help you.

Build LightGBM in Mac:

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

Then install:

cd ../python-packages  
sudo python setup.py install --precompile

As stated by @ecodan, you might need to force Mac to use GCC and G++ instead of the default compiler. So instead of building with cmake .., try:

cmake -DCMAKE_C_COMPILER=/usr/local/Cellar/gcc/6.1.0/bin/gcc-6 -DCMAKE_CXX_COMPILER=/usr/local/Cellar/gcc/6.1.0/bin/g++-6 ..

ajusting the versions to match yours.

like image 22
demianzhang Avatar answered Oct 07 '22 06:10

demianzhang


I had the same exact problem on M1 MAC. I have tried to import it through Jupiter notebook. This command solved the problem:

conda install lightgbm
like image 45
Žygimantas Avatar answered Oct 07 '22 06:10

Žygimantas


On MAC you need to install open-mpi:

brew install open-mpi
like image 35
Khaled Avatar answered Oct 07 '22 07:10

Khaled


Same error, different source: seems like I had the gcc 8 version installed, and it needs gcc 7.

It worked by switching it back to last gcc 7 version:

brew switch gcc 7.3.0_1
like image 31
Benoit R Avatar answered Oct 07 '22 07:10

Benoit R


For users with macports, replace the beginning of the brew solution with:

  1. port install gcc7 cmake
  2. export CXX=g++-mp-7 CC=gcc-mp-7
like image 40
Emre Avatar answered Oct 07 '22 07:10

Emre


On MacOS High Sierra with MacPorts installed, I did the following:

  1. Install clang-5.0 using MacPorts
  2. Inside the /build directory, run cmake -DCMAKE_CXX_COMPILER=clang++-mp-5.0 -DCMAKE_C_COMPILER=clang-mp-5.0 ..
  3. To build the python package, go to /python_package directory and modify the setup.py script. You need to modify the function compile_cpp() at the very end that checks the case for other OS (including Mac). Before the silent_call(...), add the following two lines: cmake_cmd.append("-DCMAKE_CXX_COMPILER=clang++-mp-5.0") cmake_cmd.append("-DCMAKE_C_COMPILER=clang-mp-5.0")
  4. Run sudo python setup.py install. Enjoy
like image 38
Vladislavs Dovgalecs Avatar answered Oct 07 '22 06:10

Vladislavs Dovgalecs