Torch fails to load through python script

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.
It all works fine when I log on the first time and import torch

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.
>>> import torch
>>> torch.__version__
'1.8.0'

However, if I try to import torch through a python script (e.g. the file python_script.py that contains only the line import torch) I get teh following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/anaconda3/envs/pytorch-env/lib/python3.7/site-packages/torch/__init__.py", line 627, in <module>
    import torch.utils.data
  File "/opt/anaconda3/envs/pytorch-env/lib/python3.7/site-packages/torch/utils/data/__init__.py", line 6, in <module>
    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 <module>
    from . import _utils
  File "/opt/anaconda3/envs/pytorch-env/lib/python3.7/site-packages/torch/utils/data/_utils/__init__.py", line 45, in <module>
    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 <module>
    from dataclasses import dataclass
  File "/home/alexisshakas/git/deeplogger/dataclasses.py", line 2, in <module>
    import matplotlib.pyplot as plt
  File "/opt/anaconda3/envs/pytorch-env/lib/python3.7/site-packages/matplotlib/pyplot.py", line 36, in <module>
    import matplotlib.colorbar
  File "/opt/anaconda3/envs/pytorch-env/lib/python3.7/site-packages/matplotlib/colorbar.py", line 38, in <module>
    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 <module>
    import matplotlib.text as text
  File "/opt/anaconda3/envs/pytorch-env/lib/python3.7/site-packages/matplotlib/text.py", line 17, in <module>
    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 <module>
    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 <module>
    LayoutItem = dataclasses.make_dataclass(
AttributeError: module 'dataclasses' has no attribute 'make_dataclass'

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.

Any help is greatly appreciated ! Thanks

More info:
Running Ubuntu 20.04
python 3.7.10
torch 1.8.0

p.s. import matplotlib works fine

PyTorch tries to import dataclass from the dataclasses Python library in this line of code.
However, based on the stack trace, it seems that you are also using a Python script called dataclasses.py in your working environment:

File "/home/alexisshakas/git/deeplogger/dataclasses.py", line 2, in <module>
    import matplotlib.pyplot as plt

which is then called and tries to import matplotlib, which eventually fails.

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?

This is exactly the problem. Your insight has saved me possibly hours of work :smiley: Thank you!

1 Like