Search code examples
macoslightgbm

Importing lightgbm library on M3 Macbook returns OSError


I use M3 MacBook and pycharm, and I get an error when running import lightgbm. I searched for information and executed the following command:

brew install open-mpi
brew install libomp

However, the following error message still appears. How to solve it? Thanks.

(base) mac@bogon ~ % python
Python 3.11.6 | packaged by conda-forge | (main, Oct 3 2023, 10:37:07) [Clang 15.0.7] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import lightgbm
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/Users/mac/anaconda3/lib/python3.11/site-packages/lightgbm/__init__.py", line 8, in <module>
     from .basic import Booster, Dataset, Sequence, register_logger
   File "/Users/mac/anaconda3/lib/python3.11/site-packages/lightgbm/basic.py", line 247, in <module>
     _LIB = _load_lib()
            ^^^^^^^^^^^
   File "/Users/mac/anaconda3/lib/python3.11/site-packages/lightgbm/basic.py", line 232, in _load_lib
     lib = ctypes.cdll.LoadLibrary(lib_path[0])
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/Users/mac/anaconda3/lib/python3.11/ctypes/__init__.py", line 454, in LoadLibrary
     return self._dlltype(name)
            ^^^^^^^^^^^^^^^^^^
   File "/Users/mac/anaconda3/lib/python3.11/ctypes/__init__.py", line 376, in __init__
     self._handle = _dlopen(self._name, mode)
                    ^^^^^^^^^^^^^^^^^^^^^^^^
OSError: dlopen(/Users/mac/anaconda3/lib/python3.11/site-packages/lightgbm/lib/lib_lightgbm.so, 0x0006): tried: '/Users/mac/anaconda3/lib/python3.11/site- packages/lightgbm/lib/lib_lightgbm.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64')), '/System/Volumes/Preboot/Cryptexes/OS/Users/mac/ anaconda3/lib/python3.11/site-packages/lightgbm/lib/lib_lightgbm.so' (no such file), '/Users/mac/anaconda3/lib/python3.11/site-packages/lightgbm/lib/lib_lightgbm. so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64'))

References:

M1 Mac returns OSError: Library not loaded


Solution

  • Based on the answer from this link, problem solved by running pip uninstall lightgbm, then reinstalling lightgbm with:

    conda install \
       --yes \
       -c conda-forge \
       'lightgbm>=3.3.3'