How to fix Import Error of CyLP on M1 Mac?

727 views Asked by At

I want to use the CyLP package (https://github.com/coin-or/CyLP). My OS is macOS Big Sur 11.5.1. I am using Python 3.8.8 via anaconda3. Following the instructions in the README, I first installed cbc via homebrew. As it did not function with brew install coin-or-tools/coinor/cbc I installed it using brew install cbc. Then I installed CyLP itself via pip install cylp.

The installation succeeded. CyLP is installed in the directory /opt/homebrew/anaconda3/lib/python3.8/site-packages/cylp. However, now if I try the unittests python -m unittest discover in the CyLP directory, it gives me the error attached below.

The mentioned files are in the directory, so I do not know how to fix that. Can anybody help me?

ERROR: cy (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: cy
Traceback (most recent call last):
  File "/opt/homebrew/anaconda3/lib/python3.8/unittest/loader.py", line 470, in _find_test_path
    package = self._get_module_from_name(name)
  File "/opt/homebrew/anaconda3/lib/python3.8/unittest/loader.py", line 377, in _get_module_from_name
    __import__(name)
  File "/opt/homebrew/anaconda3/lib/python3.8/site-packages/cylp/cy/__init__.py", line 1, in <module>
    from .CyCoinIndexedVector import CyCoinIndexedVector
ImportError: dlopen(/opt/homebrew/anaconda3/lib/python3.8/site-packages/cylp/cy/CyCoinIndexedVector.cpython-38-darwin.so, 2): Symbol not found: __ZN9CoinError12printErrors_E
  Referenced from: /opt/homebrew/anaconda3/lib/python3.8/site-packages/cylp/cy/CyCoinIndexedVector.cpython-38-darwin.so
  Expected in: flat namespace
 in /opt/homebrew/anaconda3/lib/python3.8/site-packages/cylp/cy/CyCoinIndexedVector.cpython-38-darwin.so


======================================================================
ERROR: py (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: py
Traceback (most recent call last):
  File "/opt/homebrew/anaconda3/lib/python3.8/unittest/loader.py", line 470, in _find_test_path
    package = self._get_module_from_name(name)
  File "/opt/homebrew/anaconda3/lib/python3.8/unittest/loader.py", line 377, in _get_module_from_name
    __import__(name)
  File "/opt/homebrew/anaconda3/lib/python3.8/site-packages/cylp/py/__init__.py", line 1, in <module>
    from . import pivots
  File "/opt/homebrew/anaconda3/lib/python3.8/site-packages/cylp/py/pivots/__init__.py", line 3, in <module>
    from .PositiveEdgePivot import PositiveEdgePivot
  File "/opt/homebrew/anaconda3/lib/python3.8/site-packages/cylp/py/pivots/PositiveEdgePivot.py", line 9, in <module>
    from cylp.cy import CyCoinIndexedVector
  File "/opt/homebrew/anaconda3/lib/python3.8/site-packages/cylp/cy/__init__.py", line 1, in <module>
    from .CyCoinIndexedVector import CyCoinIndexedVector
ImportError: dlopen(/opt/homebrew/anaconda3/lib/python3.8/site-packages/cylp/cy/CyCoinIndexedVector.cpython-38-darwin.so, 2): Symbol not found: __ZN9CoinError12printErrors_E
  Referenced from: /opt/homebrew/anaconda3/lib/python3.8/site-packages/cylp/cy/CyCoinIndexedVector.cpython-38-darwin.so
  Expected in: flat namespace
 in /opt/homebrew/anaconda3/lib/python3.8/site-packages/cylp/cy/CyCoinIndexedVector.cpython-38-darwin.so


----------------------------------------------------------------------
Ran 2 tests in 0.000s

UPDATE 12.09.21 It still does not work, but I made progress.

First, I tried to install CBC correctly. I recognized the issue, so I installed CBC and all its dependencies from source. When I finished, I still could not install cylp with pip install cylp. Even after setting COIN_INSTALL_DIR to my installation directory, it showed error related to my architecture, what was there x86. I realized I had been trying everything on anaconda 3.8, so I changed to python 3.9. Next, I cloned the repository of cylp and install it manually with pip3 install.

Now it displayed my architecture and did not ignore .dylib files anymore. But now I get this error:

  cylp/cpp/ICbcModel.cpp:35:9: error: no matching function for call to 'CbcMain0'
          CbcMain0(*this);
          ^~~~~~~~
  /Users/usr1/dist/include/coin/CbcSolver.hpp:310:6: note: candidate function not viable: requires 2 arguments, but 1 was provided
  void CbcMain0(CbcModel &babSolver, CbcSolverUsefulData &solverData);
       ^
  cylp/cpp/ICbcModel.cpp:37:16: error: no matching function for call to 'CbcMain1'
          return CbcMain1(3, argv, *this);
                 ^~~~~~~~
  /Users/usr1/dist/include/coin/CbcSolver.hpp:314:5: note: candidate function not viable: requires 4 arguments, but 3 were provided
  int CbcMain1(int argc, const char *argv[], CbcModel &babSolver,
      ^
  /Users/usr1/dist/include/coin/CbcSolver.hpp:312:5: note: candidate function not viable: requires 5 arguments, but 3 were provided
  int CbcMain1(int argc, const char *argv[], CbcModel &babSolver, int(CbcModel *currentSolver, int whereFrom), CbcSolverUsefulData &solverData);
      ^
  2 errors generated.
  error: command '/usr/bin/clang' failed with exit code 1
  ----------------------------------------
  ERROR: Failed building wheel for cylp........

2nd UPDATE 12.09.21

I think I solved the issue.

I still get this error:

  m.addConstraint(A * x == b)
EEClp6001E Unable to open file /opt/homebrew/lib/python3.9/site-packages/cylp/py/modeling/../../input/p0033.mps for reading
Clp3002W Empty problem - 0 rows, 3 columns and 0 elements
Clp0000I Optimal - objective value 0
E.Clp6001E Unable to open file /opt/homebrew/lib/python3.9/site-packages/cylp/py/../input/netlib/adlittle.mps for reading
zsh: segmentation fault  python3 -m unittest discover

But the first one is probably ignorable, and the second one is just a product of the non-existence of two files in the GitHub Repository.

So I tried to write a own example code, and it worked all fine so far.

In the last step to fix the errors occurring I inserted in ICbcModel.cpp an instance of CbcSolverUsefulData to give to CbcMain0 and CbcMain1.

I hope that if somebody has the same problem, this post will help them.

0

There are 0 answers