PyTorch Installation Error on Mac, Linux

Hi,

I am getting an error while importing torch, things used to work fine.

PyTorch was installed using conda:

Python 3.6.7 | packaged by conda-forge | (default, Feb 28 2019, 09:07:38) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/dd/anaconda2/envs/dd/lib/python3.6/site-packages/torch/__init__.py", line 181, in <module>
    from .serialization import save, load
  File "/home/dd/anaconda2/envs/dd/lib/python3.6/site-packages/torch/serialization.py", line 11, in <module>
    import tempfile
  File "/home/dd/anaconda2/envs/dd/lib/python3.6/tempfile.py", line 45, in <module>
    from random import Random as _Random
  File "/home/dd/text_sum/AlexNet/video_/examples/random.py", line 5, in <module>
    from torch.utils import data
  File "/home/dd/anaconda2/envs/dd/lib/python3.6/site-packages/torch/utils/data/__init__.py", line 3, in <module>
    from .dataset import Dataset, IterableDataset, TensorDataset, ConcatDataset, ChainDataset, Subset, random_split  # noqa: F401
  File "/home/dd/anaconda2/envs/dd/lib/python3.6/site-packages/torch/utils/data/dataset.py", line 5, in <module>
    from torch import randperm
ImportError: cannot import name 'randperm'

How to fix this issue?

Hi,

You might have multiple pytorch installs coexisting and causing issues.
I would make sure to uninstall all pytorch installs (via conda / pip etc) in your environment and then reinstall a brand new one.

I installed a brand new one on both mac & linux, in a new conda environment, still get the same error :frowning:

Which version do you install?

cuda 10 conda install linux pytorch 1.2

no cuda conda install mac pytorch 1.2

Actually, looking at the stack trace I think you have a problem with local files:
the python tempfile library, when loading picks up your local random.py file as python’s original random library.
Can you import torch from another folder? If so, you will have to rename your random.py file I’m afraid.

1 Like

thanks!! wow that really was the issue. everything is fine now :slight_smile: