Question

How to solve :ImportError: dlopen(/.../XXX.so, 0x0002): symbol not found in flat namespace '_gsl_sf_gamma'"

When I run a function in a python package, it returns:

import gala.potential as gp
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/homebrew/lib/python3.11/site-packages/gala/potential/__init__.py", line 1, in <module>
    from .potential import *
  File "/opt/homebrew/lib/python3.11/site-packages/gala/potential/potential/__init__.py", line 2, in <module>
    from .cpotential import *
ImportError: dlopen(/opt/homebrew/lib/python3.11/site-packages/gala/potential/potential/cpotential.cpython-311-darwin.so, 0x0002): symbol not found in flat namespace '_gsl_sf_gamma'

As the error message contains "_gsl_sf_gamma", so I suppose the problem is coming from the GSL library of C/C++.

I am using Mac with M2 Chip. I have installed GSL before by homebrew, so the path of GSL is

/opt/homebrew/Cellar/gsl/2.7.1

Moreover, I have also installed gcc before by homebrew.

However, the default gcc is installed from Xcode, which uses clang and its path is

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

I suppose the problem is from the inconsistency between the GSL and the gcc.

I have tried to reinstall GSL through conda (without uninstall the previous GSL), but it doesn't work.

 3  64  3
1 Jan 1970

Solution

 0

OK so this is a linking problem.

The library you are trying to import tries to call a function _gsl_sf_gamma (probably from the GSL library) but cannot find it.

The library you are trying to import is programmed in a compiled language (most likely C or C++). In these languages, libraries, work a bit differently from how they work in python. There are two ways to get functions or other values reffered to by names (symbols) from a library.

  • Static linking bundles the function from the library in the binary calling it.
  • Dynamic linking loads the library on-demand, like python does with its import statement. It is more complicated because the binary calling the function needs to find the library somewhere on the system.

So basically something was not properly linked. Probably that that cpotential.cpython-311-darwin.so expects the GSL library to already be dynamically loaded.

Now the conda environment changes a bunch of things, including where programs are going to look for dynamically linked libraries. It probably loaded a different version of cpotential.cpython-311-darwin.so which loaded GSL correctly from conda's libraries

2024-07-18
tbrugere