Unable to import librosa on binder

I was trying to get a jupyter notebook working on binder. It has a dependency on librosa. All the dependencies get installed through the requirements.txt file, but when I try to run the code, I get an error when trying to import librosa:

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
File /srv/conda/envs/notebook/lib/python3.8/site-packages/soundfile.py:151
    150 if _libname is None:
--> 151     raise OSError('sndfile library not found')
    152 _snd = _ffi.dlopen(_libname)

OSError: sndfile library not found

During handling of the above exception, another exception occurred:

OSError                                   Traceback (most recent call last)
File /srv/conda/envs/notebook/lib/python3.8/site-packages/soundfile.py:178
    177 try:  # packaged libsndfile:
--> 178     _snd = _ffi.dlopen(_os.path.join(_path, '_soundfile_data', _packaged_libname))
    179 except OSError:  # try system-wide libsndfile:
    180     # Homebrew on Apple M1 uses a `/opt/homebrew/lib` instead of
    181     # `/usr/local/lib`. We are making sure we pick that up.

OSError: cannot load library '/srv/conda/envs/notebook/lib/python3.8/site-packages/_soundfile_data/libsndfile.so': /srv/conda/envs/notebook/lib/python3.8/site-packages/_soundfile_data/libsndfile.so: cannot open shared object file: No such file or directory

During handling of the above exception, another exception occurred:

OSError                                   Traceback (most recent call last)
Cell In[1], line 1
----> 1 import librosa
      2 import librosa.display
      3 import numpy as np

File /srv/conda/envs/notebook/lib/python3.8/site-packages/librosa/__init__.py:209
    207 # And all the librosa sub-modules
    208 from ._cache import cache
--> 209 from . import core
    210 from . import beat
    211 from . import decompose

File /srv/conda/envs/notebook/lib/python3.8/site-packages/librosa/core/__init__.py:6
      3 """ Core IO and DSP functions"""
      5 from .convert import *  # pylint: disable=wildcard-import
----> 6 from .audio import *  # pylint: disable=wildcard-import
      7 from .spectrum import *  # pylint: disable=wildcard-import
      8 from .pitch import *  # pylint: disable=wildcard-import

File /srv/conda/envs/notebook/lib/python3.8/site-packages/librosa/core/audio.py:8
      5 import pathlib
      6 import warnings
----> 8 import soundfile as sf
      9 import audioread
     10 import numpy as np

File /srv/conda/envs/notebook/lib/python3.8/site-packages/soundfile.py:189
    186             _snd = _ffi.dlopen(_os.path.join(_hbrew_path, _libname))
    187         else:
    188             # Try explicit file name, if the general does not work (e.g. on nixos)
--> 189             _snd = _ffi.dlopen(_libname)
    191 __libsndfile_version__ = _ffi.string(_snd.sf_version_string()).decode('utf-8', 'replace')
    192 if __libsndfile_version__.startswith('libsndfile-'):

OSError: cannot load library 'libsndfile.so': libsndfile.so: cannot open shared object file: No such file or directory

Import fails due to sndfile library missing on the server. How do I fix this? Attaching requirements.txt contents below:

jenkspy==0.3.2
librosa==0.9.2
matplotlib==3.6.2
numpy==1.23.5

Can you share the requirements.txt file? Have you install GitHub - conda-forge/libsndfile-feedstock: A conda-smithy repository for libsndfile.?

Added requirements.txt @rgaiacs. I am not using conda for managing my dependencies, I used pip and it worked well on my local virtual env.

Maybe because you have the libraries from something else?

Anyways, here you may be able to get away with continuing to not use conda if you add an apt.txt configuration file and list libsndfile1 in it.

As I suspect Nick is going to point out though you’ll have an easier time relying on conda.

2 Likes

soundfile in turn requires system libraries which cannot be installed with pip.

Your computer probably already has them for… sound. A random container in the cloud, such as you get on binder, often won’t.

You can ensure it is installed on the container with an apt.txt:

libsndfile

am i that predictable? I wasn’t even going to, as someone had already already suggested.

But since here we are: the conda-forge distribution will provide all required dependencies.

So, the environment.yml would be:

channels:
- conda-forge
- nodefaults
dependencies:
- jenkspy==0.3.2
- librosa==0.9.2
- matplotlib-base==3.6.2  # -base avoids the huge qt backend
- numpy==1.23.5

(tested locally, does appear to solve, so all packages/versions are available)

3 Likes

I saw you were typing away. And I’ve learned from your solutions it usually is much easier.

2 Likes

Thanks @fomightez! Will keep in mind to use conda from next time.

Thanks @bollwyvl! That helped.

2 Likes