ImportError: No module named 'torchvision.datasets.mnist'

11,168

If you're using anaconda distribution, first install torchvision using:

$ conda install -c conda-forge torchvision

If the package is not installed, then it will be installed. Else, it will throw the message

# All requested packages already installed.

After this, try to import the torchvision.datasets as you mentioned.

In [1]: from torchvision import datasets 

In [2]: dir(datasets)  
Out[2]: 
['CIFAR10',
 'CIFAR100',
 'CocoCaptions',
 'CocoDetection',
 'DatasetFolder',
 'EMNIST',
 'FakeData',
 'FashionMNIST',
 'ImageFolder',
 'LSUN',
 'LSUNClass',
 'MNIST',
 'Omniglot',
 'PhotoTour',
 'SEMEION',
 'STL10',
 'SVHN',
 ....,
 ....
]

As you can see from above listing of dir(datasets), the dataset class for MNIST is listed which will be the case when the torchvision package is installed correctly.

Share:
11,168

Related videos on Youtube

Nehal Ahuja
Author by

Nehal Ahuja

Updated on June 04, 2022

Comments

  • Nehal Ahuja
    Nehal Ahuja almost 2 years

    Even after installing pytorch, this error is coming for this line.

    from torchvision import datasets
    
    • kHarshit
      kHarshit over 5 years
      Just to confirm, did you install torchvision as well? pip3 install torchvision
    • Monster
      Monster over 5 years
      Try reinstalling torchvision with pip install --upgrade --force-reinstall torchvision
  • dennlinger
    dennlinger over 5 years
    For any other distribution (not Anaconda), the PyTorch homepage provides a convenient tool that lets you input your environment, and return a command to install the relevant package, if you just scroll down a little bit.