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

ThomasHezard: <p>Hello,</p>
<p>I’ve been struggling with <code>torchaudio</code> since I migrated to my new macBook Pro.</p>
<p>The problem is the following: <code>torchaudio</code> can not initialize the <code>sox</code> backend when</p>
<ul>
<li>the Python environment is managed by <code>uv</code>,</li>
<li>and I debug from PyCharm or VSCode (the two IDEs I use for Python, I did not test any other),</li>
<li>even though <code>sox</code> is installed and the scripts run perfectly from the terminal (with the environment managed by <code>uv</code>).</li>
</ul>
<p>The issue seems related to <code>uv</code> as I don’t have any problem if I manage the environment manually with <code>venv</code> and <code>pip</code>.<br>
More details in <a href="https://github.com/astral-sh/uv/issues/9941" rel="noopener nofollow ugc">my issue on <code>uv</code> repo</a>.</p>
<p>However, the problems seems related to my environment somehow:</p>
<ul>
<li>I did not encounter the issue on my previous computer (also a macBook Pro Apple Silicon).</li>
<li>If I work in a bash session with a much simpler environment (I have a quite complicated <code>.zprofile</code> file but no <code>.bash_profile</code> or <code>.profile</code> file), the problem is even worse as it appears even in scripts launched from the terminal with the environment managed by <code>uv</code>.</li>
</ul>
<p>My questions are the following (I tried to look at the source code but couldn’t figure out the answers):</p>
<ul>
<li>How does <code>torchaudio</code> links to the <code>sox</code> backend?</li>
<li>In what conditions does <code>torchaudio.list_audio_backends()</code> return an empty list even though <code>shutil.which("sox")</code> returns the path to <code>sox</code> properly?</li>
</ul>
<p>Thank you for your help <img src="https://discuss.pytorch.org/images/emoji/apple/pray.png?v=12" title=":pray:" class="emoji" alt=":pray:" loading="lazy" width="20" height="20"></p>

ptrblck: <aside class="quote no-group" data-username="ThomasHezard" data-post="1" data-topic="214313">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/thomashezard/48/74261_2.png" class="avatar"> ThomasHezard:</div>
<blockquote>
<p>How does <code>torchaudio</code> links to the <code>sox</code> backend?</p>
</blockquote>
</aside>
<p>During the runtime it tries to load it via: <a href="https://github.com/pytorch/audio/blob/a6b0a140cc13216975e8922093459019537bb80a/src/torchaudio/_backend/utils.py#L20-L29"><code>lazy_import_sox_ext().is_available()</code></a> → <a href="https://github.com/pytorch/audio/blob/a6b0a140cc13216975e8922093459019537bb80a/src/torchaudio/_extension/__init__.py#L50-L56"><code>_LazyImporter("_torchaudio_sox", _init_sox)</code></a> → <a href="https://github.com/pytorch/audio/blob/a6b0a140cc13216975e8922093459019537bb80a/src/torchaudio/_extension/utils.py#L64-L106"><code>_init_sox/_import_sox_ext</code></a> → <a href="https://github.com/pytorch/audio/blob/a6b0a140cc13216975e8922093459019537bb80a/src/torchaudio/_extension/utils.py#L26-L61"><code>_load_lib</code></a> → <a href="https://github.com/pytorch/pytorch/blob/9283c40ba8e6adf55db3d12f7451d86bb9c68632/torch/_ops.py#L1330-L1357"><code>torch.ops.load_library</code></a>.</p>

ThomasHezard: <p>Hello,</p>
<p>Thanks for the input!</p>
<p>When in debug mode, the line raising is <a href="https://github.com/pytorch/pytorch/blob/9283c40ba8e6adf55db3d12f7451d86bb9c68632/torch/_ops.py#L1356" rel="noopener nofollow ugc"><code>ctypes.CDLL(path)</code></a>, where path is “/XXX/.venv/lib/python3.10/site-packages/torchaudio/lib/libtorchaudio_sox.so”.<br>
The error thrown is the following:</p>
<pre><code class="lang-auto">('dlopen(/XXX/.venv/lib/python3.10/site-packages/torchaudio/lib/libtorchaudio_sox.so, 0x0006): Library not loaded: @rpath/libsox.dylib
  Referenced from: &lt;56CD0903-8343-3EFF-AFBF-9F60216E7F09&gt; /XXX/.venv/lib/python3.10/site-packages/torchaudio/lib/libtorchaudio_sox.so
  Reason: no LC_RPATH's found',)
</code></pre>
<p>The <code>libsox.dylib</code> file is in <code>/opt/homebrew/lib/libsox.dylib</code>. 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?</p>
<p>I guess this has something to do with the environment variables but I can’t figure out what and where is the problem.</p>
<p>Thank you all <img src="https://discuss.pytorch.org/images/emoji/apple/pray.png?v=12" title=":pray:" class="emoji" alt=":pray:" loading="lazy" width="20" height="20"></p>

ThomasHezard: <p>I found the solution after some time digging the web.</p>
<p>This was indeed a problem of <code>LC_RPATH</code>. The problem disappeared when I deleted and re-installed my Python interpreters.</p>
<p>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 <code>LC_RPATH</code>.</p>
<p>Since I re-installed the Python interpreters (in my case with <code>uv</code>) with a clean and fully functional environment, everything works fine.</p>