No module named 'torchaudio.backend.common'

gernophil: <p>I am trying to use <code>pytorch-nightly</code> with a project on my macOS. <code>pytorch</code> stable can be used, but the results could be better. So, I wanted to give <code>pytorch-nightly</code> a try. If I run the same script using <code>pytorch-nightly</code>, I get the error:</p>
<pre><code class="lang-auto">...
ModuleNotFoundError: No module named 'torchaudio.backend.common'
</code></pre>
<p>Is this module for some reason not part of <code>pytorch-nightly</code>?</p>
<p>I tried to install via conda using</p>
<pre><code class="lang-auto">mamba create -n pytorch-nightly pytorch-nightly::pytorch pytorch-nightly::torchvision pytorch-nightly::torchaudio -c pytorch-nightly
</code></pre>
<p>and</p>
<pre><code class="lang-auto">mamba create -n pytorch-nightly pytorch-nightly::pytorch torchvision torchaudio -c pytorch-nightly
</code></pre>
<p>And I also tried via pip</p>
<pre><code class="lang-auto">pip3 install --pre torch torchvision torchaudio --index-url
</code></pre>
<p>Of course, I used a fresh environment everytime. I just executed <code>import torchaudio.backend.common</code> in the interactive python console. With every <code>pytorch-nightly</code> env I get the <code>ModuleNotFoundError</code>. Doing the same with just pytorch works.</p>
<p>Any ideas?</p>

ptrblck: <p><a href="https://github.com/pytorch/audio/pull/3549">This PR</a> moved some modules around to <code>_backend</code>.<br>
However, <code>torchaudio.backend.common</code> still works for me in <code>2.1.0.dev20230817+cu121</code>, which should already contain the linked PR:</p>
<pre><code class="lang-python">torchaudio.backend.common
&lt;module 'torchaudio.backend._common' from '/home/pbialecki/miniforge3/envs/nightly_pip_cu121/lib/python3.10/site-packages/torchaudio/backend/_common.py'&gt;

torchaudio.__version__
'2.1.0.dev20230817+cu121'
</code></pre>

gernophil: <p>Hmm, that’s weird.<br>
I get (almost) the same output as you:</p>
<pre><code class="lang-auto">&gt;&gt;&gt; torchaudio.backend.common
&lt;module 'torchaudio.backend._common' from '/Users/gernophil/miniconda3/envs/pytorch-nightly/lib/python3.11/site-packages/torchaudio/backend/_common.py'&gt;
&gt;&gt;&gt; torchaudio.__version__
'2.1.0.dev20230823'
</code></pre>
<p>Still I cannot run this import command:</p>
<pre><code class="lang-auto">from torchaudio.backend.common import AudioMetaData
</code></pre>
<p>It’s used by a pyannote fork I use and is does work with the non-nightly version.</p>
<p>However, by importing <code>tfrom torchaudio.backend._common import AudioMetaData</code> I got the message that it’s been moved to <code>torchaudio.AudioMetaData</code>. Should be doable now :).</p>

ptrblck: <p>Try to replace the <code>backend</code> module with <code>_backend</code> as described in the PR and see if this would work.</p>

moto: <p>This is a regression. I intended to keep the existing import compatible. I will fix it.</p>
<p>For the future, if the backward compatibility is not an issue, please use <code>torchaudio.AudioMetaData</code>. (no <code>backend</code> or <code>_backend</code>)</p>

gernophil: <p>Has the package changed after the release of 2.1.0?<br>
I ran my script using 2.1.0 (installed via pip in a fresh venv) on Oct 6th and got the following error:<br>
<code>No module named 'torchaudio.backend.common'</code><br>
Yesterday I installed it again in a fresh venv (same version 2.1.0) and now I don’t get this error anymore, but only a warning.<br>
How can this happen using the same version? Are there minor changes after the release?</p>

moto: <blockquote>
<p>How can this happen using the same version?</p>
</blockquote>
<p>It should not happen, if it is really a 2.1.0 release. The only way that it happens, which I can think of, is that an old nightly build was present in the environment.</p>
<blockquote>
<p>Are there minor changes after the release?</p>
</blockquote>
<p>No, there wasn’t. We are preparing 2.1.1 minor release, but we don’t change what’s released.</p>

ptrblck: <p>The latest <code>torchaudio==2.1.0+cu121</code> release also still works for me:</p>
<pre><code class="lang-python">import torchaudio

torchaudio.__version__
Out[10]: '2.1.0+cu121'

torchaudio.AudioMetaData
Out[11]: torchaudio._backend.common.AudioMetaData

from torchaudio._backend.common import AudioMetaData

AudioMetaData
Out[13]: torchaudio._backend.common.AudioMetaData
</code></pre>

gernophil: <p>That’s really weird then. I thought I freshly created the venv. I must have accidentally used a wrong venv. Who knows. Took me almost a month till I realized I can actually use torchaudio 2.1.0 with my script <img src="https://discuss.pytorch.org/images/emoji/apple/sweat_smile.png?v=12" title=":sweat_smile:" class="emoji" alt=":sweat_smile:" loading="lazy" width="20" height="20">. Thanks for the great work by the way. MPS works so much better using 2.1.0.</p>

gernophil: <p>Ok, seems like it’s not that weird. I just started my script in an x86_64 venv (torchaudio 2.1.0 installed at the same hour as in the arm64 venv). In this venv I still get the <code>No module named 'torchaudio.backend.common'</code> error. Is it possible that there is a difference between the macOS arm64 and the x86_64 package in that point?</p>

gernophil: <p>there is indeed a difference between the torchaudio package for arm64 and x86_64:</p>
<pre><code class="lang-auto"># arm64
Python 3.9.18 (main, Aug 24 2023, 18:16:58) 
[Clang 15.0.0 (clang-1500.0.40.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import torchaudio.backend.common
&gt;&gt;&gt; import torch
&gt;&gt;&gt; print(torch.__version__)
2.1.0
&gt;&gt;&gt; import torchaudio
&gt;&gt;&gt; print(torchaudio.__version__)
2.1.0

# x86_64
Python 3.9.18 (main, Aug 24 2023, 18:16:58) 
[Clang 15.0.0 (clang-1500.0.40.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import torchaudio.backend.common
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
ModuleNotFoundError: No module named 'torchaudio.backend.common'
&gt;&gt;&gt; import torch
&gt;&gt;&gt; print(torch.__version__)
2.1.0
&gt;&gt;&gt; import torchaudio
&gt;&gt;&gt; print(torchaudio.__version__)
2.1.0
</code></pre>

Aml_hassan1: <p>I have a very similar issue.<br>
I’m trying to run yolov7 and I keep getting this error:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "/media/aml/happy/ubuntu dowenloads/yolov7-main/detect.py", line 7, in &lt;module&gt;
    import torch.backends.cudnn as cudnn
ModuleNotFoundError: No module named 'torch.backends'
</code></pre>
<p>I have torch installed and I can import it with no problem but it doesn’t recognize ‘torch.backends’</p>
<p>if you found a solution for your issue, please share it, maybe it can help me.</p>

ptrblck: <p>The <code>torchaudio</code> issue is most likely unrelated to yours. Which PyTorch version are you using?</p>

Aml_hassan1: <p>Thank you very much for your response.<br>
I can’t access torch.version</p>
<pre><code class="lang-auto">&gt;&gt;&gt; import torch
&gt;&gt;&gt; print(torch.__version__)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
AttributeError: module 'torch' has no attribute '__version__'
&gt;&gt;&gt; print(torch.version.__version__)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
AttributeError: module 'torch' has no attribute 'version'
</code></pre>
<p>I think I might have installed the wrong torch?!</p>

ptrblck: <p>Yes, maybe you’ve installed the wrong package or you might also be in a working directory containing a script called <code>torch.py</code>. Avoid these name conflicts or switch to another directory.</p>