nwhitehead / pyfluidsynth

Python bindings for FluidSynth
GNU Lesser General Public License v2.1
197 stars 56 forks source link

raise ImportError, "Couldn't find the FluidSynth library." on M1 Macbook #40

Closed windfall-s closed 4 months ago

windfall-s commented 2 years ago

I use MacOS 12.3 (Monterey) on MacBook Pro (14-inch, 2021).

I installed fluidsynth by brew install fluidsynth, but my fluidsynth.py returned

  File "/Users/<myusername>/.pyenv/versions/<myPyenvName>/lib/python3.9/site-packages/fluidsynth.py", line 44, in <module>
    raise ImportError("Couldn't find the FluidSynth library.")
ImportError: Couldn't find the FluidSynth library.

When I ran import fluidsynth.

I tried adding /opt/homebrew/Cellar/fluid-synth/2.2.8_1/lib dir in my $PATH (the directory contains libfluidsynth.3.dylib), but the error message persisted.

Interestingly, when I import fluidsynth on my non-M1 macbook, the code worked as is.

Does anyone have an idea on what is wrong in my setting?

windfall-s commented 2 years ago

I managed to resolve it.

The ctypes.util.find_library() call in fluidsynth.py calls an underlying method dyld_find(), which didn't check opt/homebrew/Celler/ subdirectories.

print()ing all the paths revealed /Users/<myUserName>/lib/ is one of the search directory, and adding a symlink of libfluidsynth.dylib resolved.

ln -s /opt/homebrew/Cellar/fluid-synth/2.2.8_1/lib/libfluidsynth.dylib /Users/<myUserName>/lib/libfluidsynth.dylib

Although this is not at all a clean solution, it at least worked

davidgraymi commented 1 year ago

Similarly, I downloaded pyfluidsynth using pip in miniconda3 and was able to resolve this issue by adding a symlink to my miniconda3 directory (which has a very similar architecture to homebrew, thankfully).

ln -s /opt/homebrew/Cellar/fluid-synth/2.3.0/lib/libfluidsynth.dylib /Users/<myUserName>/miniconda3/lib/libfluidsynth.dylib

I also did the same for the cmake files, pkgconfig, include, bin, and share to make everything accessible to miniconda. It's not necessary though.

DahnJ commented 1 year ago

For me, it was enough to add the folder with libfluidsynth.dylib into the DYLIB_LIBRARY_PATH environmental variable.

In my case specifically, this meant adding the following line into my ~/.zshrc:

export DYLD_LIBRARY_PATH="/opt/homebrew/Cellar/fluid-synth/2.3.1_1/lib/:$DYLD_LIBRARY_PATH"

Thank you for the other answers though, they pointed me in the right direction to solve this 🙇

cclauss commented 4 months ago

This should work on Apple Silicon ARM.

export DYLD_LIBRARY_PATH="$(brew --prefix fluid-synth)/lib/:$DYLD_LIBRARY_PATH"

In our GitHub Action, I needed to https://github.com/nwhitehead/pyfluidsynth/blob/818912d41a37a151043c20ec52c98fc95137c58f/.github/workflows/ci.yml#L26

And also get the change merged in:

@windfall-s Can this issue be closed?

cclauss commented 4 months ago

@nwhitehead I think this can be closed.

Everyone above was able to get it to work and the solution is documented in our GitHub Actions.