Torch fails to load through python script

username: shakasaki
cooked: <p>I’ve installed torch on a small server that is to be used by ~10 people. I have a separate user set up where I’m installing all packages for all users using conda. I’ve installed pytorch there, in a virtual environment called pytorch-env.<br>
It all works fine when I log on the first time and import torch</p>
<pre><code class="lang-auto">conda activate pytorch-env
python
Python 3.7.10 | packaged by conda-forge | (default, Feb 19 2021, 16:07:37) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'1.8.0'
</code></pre>
<p>However, if I try to import torch through a python script (e.g. the file <strong>python_script.py</strong> that contains only the line <strong>import torch</strong>) I get teh following error:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/opt/anaconda3/envs/pytorch-env/lib/python3.7/site-packages/torch/__init__.py", line 627, in &lt;module&gt;
    import torch.utils.data
  File "/opt/anaconda3/envs/pytorch-env/lib/python3.7/site-packages/torch/utils/data/__init__.py", line 6, in &lt;module&gt;
    from .dataloader import DataLoader, _DatasetKind, get_worker_info
  File "/opt/anaconda3/envs/pytorch-env/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 21, in &lt;module&gt;
    from . import _utils
  File "/opt/anaconda3/envs/pytorch-env/lib/python3.7/site-packages/torch/utils/data/_utils/__init__.py", line 45, in &lt;module&gt;
    from . import worker, signal_handling, pin_memory, collate, fetch
  File "/opt/anaconda3/envs/pytorch-env/lib/python3.7/site-packages/torch/utils/data/_utils/worker.py", line 10, in &lt;module&gt;
    from dataclasses import dataclass
  File "/home/alexisshakas/git/deeplogger/dataclasses.py", line 2, in &lt;module&gt;
    import matplotlib.pyplot as plt
  File "/opt/anaconda3/envs/pytorch-env/lib/python3.7/site-packages/matplotlib/pyplot.py", line 36, in &lt;module&gt;
    import matplotlib.colorbar
  File "/opt/anaconda3/envs/pytorch-env/lib/python3.7/site-packages/matplotlib/colorbar.py", line 38, in &lt;module&gt;
    from matplotlib import _api, collections, cm, colors, contour, ticker
  File "/opt/anaconda3/envs/pytorch-env/lib/python3.7/site-packages/matplotlib/contour.py", line 18, in &lt;module&gt;
    import matplotlib.text as text
  File "/opt/anaconda3/envs/pytorch-env/lib/python3.7/site-packages/matplotlib/text.py", line 17, in &lt;module&gt;
    from .textpath import TextPath  # Unused, but imported by others.
  File "/opt/anaconda3/envs/pytorch-env/lib/python3.7/site-packages/matplotlib/textpath.py", line 8, in &lt;module&gt;
    from matplotlib import _text_layout, dviread, font_manager, rcParams
  File "/opt/anaconda3/envs/pytorch-env/lib/python3.7/site-packages/matplotlib/_text_layout.py", line 10, in &lt;module&gt;
    LayoutItem = dataclasses.make_dataclass(
AttributeError: module 'dataclasses' has no attribute 'make_dataclass'

</code></pre>
<p>When I try again to import torch with my first approach (simply opening a python shell and running import torch) I get the same error. So it loads well the first time, but as soon as I try loading through a script, it then gives me the same error.</p>
<p>Any help is greatly appreciated ! Thanks</p>
<p>More info:<br>
Running Ubuntu 20.04<br>
python 3.7.10<br>
torch 1.8.0</p>
<p>p.s. <strong>import matplotlib</strong> works fine</p>

username: ptrblck
cooked: <p>PyTorch tries to import <code>dataclass</code> from the <code>dataclasses</code> Python library in <a href="https://github.com/pytorch/pytorch/blob/5ba4cfb7bfbfe5f1be303a5326bb3d2397e011ea/torch/utils/data/_utils/worker.py#L11">this line of code</a>.<br>
However, based on the stack trace, it seems that you are also using a Python script called <code>dataclasses.py</code> in your working environment:</p>
<pre><code class="lang-python">File "/home/alexisshakas/git/deeplogger/dataclasses.py", line 2, in &lt;module&gt;
    import matplotlib.pyplot as plt
</code></pre>
<p>which is then called and tries to import <code>matplotlib</code>, which eventually fails.</p>
<p>Could you rename your file for the sake of debugging and maybe change its name so that it doesn’t create conflicts with Python’s built-in classes?</p>

username: shakasaki
cooked: <p>This is exactly the problem. Your insight has saved me possibly hours of work <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"> Thank you!</p>