Please Help: `torchaudio` does not find the `sox` backend on macOS

Hello,

I’ve been struggling with torchaudio since I migrated to my new macBook Pro.

The problem is the following: torchaudio can not initialize the sox backend when

  • the Python environment is managed by uv,
  • and I debug from PyCharm or VSCode (the two IDEs I use for Python, I did not test any other),
  • even though sox is installed and the scripts run perfectly from the terminal (with the environment managed by uv).

The issue seems related to uv as I don’t have any problem if I manage the environment manually with venv and pip.
More details in my issue on uv repo.

However, the problems seems related to my environment somehow:

  • I did not encounter the issue on my previous computer (also a macBook Pro Apple Silicon).
  • If I work in a bash session with a much simpler environment (I have a quite complicated .zprofile file but no .bash_profile or .profile file), the problem is even worse as it appears even in scripts launched from the terminal with the environment managed by uv.

My questions are the following (I tried to look at the source code but couldn’t figure out the answers):

  • How does torchaudio links to the sox backend?
  • In what conditions does torchaudio.list_audio_backends() return an empty list even though shutil.which("sox") returns the path to sox properly?

Thank you for your help :pray:

During the runtime it tries to load it via: lazy_import_sox_ext().is_available()_LazyImporter("_torchaudio_sox", _init_sox)_init_sox/_import_sox_ext_load_libtorch.ops.load_library.

Hello,

Thanks for the input!

When in debug mode, the line raising is ctypes.CDLL(path), where path is “/XXX/.venv/lib/python3.10/site-packages/torchaudio/lib/libtorchaudio_sox.so”.
The error thrown is the following:

('dlopen(/XXX/.venv/lib/python3.10/site-packages/torchaudio/lib/libtorchaudio_sox.so, 0x0006): Library not loaded: @rpath/libsox.dylib
  Referenced from: <56CD0903-8343-3EFF-AFBF-9F60216E7F09> /XXX/.venv/lib/python3.10/site-packages/torchaudio/lib/libtorchaudio_sox.so
  Reason: no LC_RPATH's found',)

The libsox.dylib file is in /opt/homebrew/lib/libsox.dylib. Any idea why this error may raise when running the script from PyCharm Run/Debug UI buttons but not when calling the script from terminal?

I guess this has something to do with the environment variables but I can’t figure out what and where is the problem.

Thank you all :pray:

I found the solution after some time digging the web.

This was indeed a problem of LC_RPATH. The problem disappeared when I deleted and re-installed my Python interpreters.

I am not sure what happened, my guess is that my libraries paths were not setup correctly when I originally installed my Python interpreters, which resulting in missing LC_RPATH.

Since I re-installed the Python interpreters (in my case with uv) with a clean and fully functional environment, everything works fine.