pythonazurelibrosalibsndfile

Using librosa in an azure function app written in python


I'm working an an azure function in python that uses librosa to visualize some audio data. It works fine locally on my windows box in vscode. Remote build from the command line with

func azure functionapp publish functionappname --build remote

succeeds, but when a client posts data to it, it fails with

OSError: sndfile library not found

File "/home/site/wwwroot/VisualizeDemo/__init__.py", line 20, in <module> import librosa 
File "/home/site/wwwroot/.python_packages/lib/site-packages/librosa/__init__.py", line 12, in <module> from . import core 
File "/home/site/wwwroot/.python_packages/lib/site-packages/librosa/core/__init__.py", line 126, in <module> from .audio import * # pylint: disable=wildcard-import 
File "/home/site/wwwroot/.python_packages/lib/site-packages/librosa/core/audio.py", line 10, in <module> import soundfile as sf 
File "/home/site/wwwroot/.python_packages/lib/site-packages/soundfile.py", line 142, in <module> raise OSError('sndfile library not found')

I understand that librosa depends on libsndfile, and that

pip install librosa

also installs libsndfile on my local, and that libsndfile is not included in the wheels I upload to azure. I ran

apt-get install libsndfile1

on the function host, to install the library there, and that seems to have succeeded:

root@7d0b717a5bb5:/home/site/wwwroot# apt-get install libsndfile1
Reading package lists... Done
Building dependency tree
Reading state information... Done
libsndfile1 is already the newest version (1.0.28-6).
0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.

But the function still can't find it. Is there a different way to install libsndfile so it will be visible to the function, or a way to tell the function where to look for that library? The function app is running on a regular app service plan, not the serverless consumption plan.


Solution

  • By default, Azure Function on Linux runs in a default container. So installing via apt-get install libsndfile1 in the Function host would have no effect on what's inside the container. For such scenario, you should consider Creating function using a custom container.

    Note: Custom image is not supported in Consumption plan. It would need Premium plan or a Dedicated (App Service) plan.