bodono / scs-python

Python interface for SCS
MIT License
41 stars 33 forks source link

Installation issue on mac #55

Closed SteveDiamond closed 11 months ago

SteveDiamond commented 2 years ago

Please read this first

If you are have a problem that SCS struggles with you can pass a string to the write_data_filename argument and SCS will dump a file containing the problem to disk. Zip the file and send it to us or attach it to this bug report for easy reproduction of the issue.

A common cause of issues is not linking BLAS/LAPACK libraries correctly. If you are having this issue please search for resources on installing and linking these libraries first. You can try openblas if you need a BLAS library.

Specifications

Description

Someone with a vanilla Python 3 setup tried to install SCS and got an import error.

How to reproduce

The problem occurred on two different machines with vanilla (no conda or venv) Python 3 setups.

Additional information

Output

>>> import cvxpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cvxpy/__init__.py", line 27, in <module>
    from cvxpy.problems.problem import Problem
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cvxpy/problems/problem.py", line 35, in <module>
    from cvxpy.reductions import InverseData
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cvxpy/reductions/__init__.py", line 32, in <module>
    from cvxpy.reductions.solvers.solving_chain import SolvingChain
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cvxpy/reductions/solvers/solving_chain.py", line 21, in <module>
    from cvxpy.reductions.solvers import defines as slv_def
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cvxpy/reductions/solvers/defines.py", line 98, in <module>
    INSTALLED_SOLVERS = installed_solvers()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cvxpy/reductions/solvers/defines.py", line 87, in installed_solvers
    if solver.is_installed():
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cvxpy/reductions/solvers/solver.py", line 59, in is_installed
    self.import_solver()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cvxpy/reductions/solvers/conic_solvers/scs_conif.py", line 134, in import_solver
    import scs
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/scs/__init__.py", line 5, in <module>
    import _scs_direct
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/_scs_direct.cpython-38-darwin.so, 2): Library not loaded: @rpath/libmkl_rt.1.dylib
  Referenced from: /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/_scs_direct.cpython-38-darwin.so
  Reason: image not found
bodono commented 2 years ago

SCS relies on Numpy to tell it where these libraries are on your system, using the get_info commands:

from numpy.distutils.system_info import get_info
print(get_info("blas_opt"))  # best blas install
print(get_info("lapack_opt"))  # best lapack install
print(get_info("blas"))  # fall back blas install
print(get_info("lapack"))  # fall back lapack install

What do the outputs of running those commands yield?

After that: Does libmkl_rt exist on the machine?

If not the user needs to install a blas / lapack lib and reinstall SCS.

If it does exist, then it sounds like something is wrong with the DYLD_LIBRARY_PATH environment variable (try also LD_RUNPATH_SEARCH_PATH, LD_LIBRARY_PATH etc., I'm not exactly sure which one is best). If you append the location of the mkl library to the env variable it should pick it up.

enricogrande commented 2 years ago

I had a similar problem with scs>=3.0.0, Mac OS X 10.14.6 and python 3.7.7.

The error message I receive is the following

In [2]: import scs
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-2-97fdeadba5ba> in <module>
----> 1 import scs

~/virtualenvs/dev/lib/python3.7/site-packages/scs/__init__.py in <module>
      2 from warnings import warn
      3 from scipy import sparse
----> 4 import _scs_direct
      5 
      6 __version__ = _scs_direct.version()

ImportError: dlopen(/Users/enrico/virtualenvs/dev/lib/python3.7/site-packages/_scs_direct.cpython-37m-darwin.so, 2): Symbol not found: _aligned_alloc
  Referenced from: /Users/enrico/virtualenvs/dev/lib/python3.7/site-packages/scs/.dylibs/libgomp.1.dylib (which was built for Mac OS X 10.15)
  Expected in: /usr/lib/libSystem.B.dylib
 in /Users/enrico/virtualenvs/dev/lib/python3.7/site-packages/scs/.dylibs/libgomp.1.dylib

Running the commands suggested by @bodono produces

Python 3.7.7 (v3.7.7:d7c567b08f, Mar 10 2020, 02:56:16) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.31.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from numpy.distutils.system_info import get_info
   ...: print(get_info("blas_opt"))  # best blas install
   ...: print(get_info("lapack_opt"))  # best lapack install
   ...: print(get_info("blas"))  # fall back blas install
   ...: print(get_info("lapack"))  # fall back lapack install
{'extra_compile_args': ['-msse3', '-I/System/Library/Frameworks/vecLib.framework/Headers'], 'extra_link_args': ['-Wl,-framework', '-Wl,Accelerate'], 'define_macros': [('NO_ATLAS_INFO', 3), ('HAVE_CBLAS', None)]}
{'extra_compile_args': ['-msse3', '-I/System/Library/Frameworks/vecLib.framework/Headers'], 'extra_link_args': ['-Wl,-framework', '-Wl,Accelerate'], 'define_macros': [('NO_ATLAS_INFO', 3), ('HAVE_CBLAS', None)]}
/var/folders/9r/gd86060902l6q82mdjnv8sgr0000gn/T/tmp4h4phrhy/source.c:1:10: fatal error: 'cblas.h' file not found
#include <cblas.h>
         ^~~~~~~~~
1 error generated.
{'libraries': ['blas', 'blas'], 'library_dirs': ['/usr/lib'], 'include_dirs': ['/usr/local/include']}
{'libraries': ['lapack', 'lapack'], 'library_dirs': ['/usr/lib'], 'language': 'f77'}

If I downgrade to version 2.1.4 I do not have problems.

Thanks.

enzbus commented 11 months ago

This should be fixed now, the whole packaging code has been rewritten and it has smarter ways to detect blas on your system.