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

Nehal Ahuja picture Nehal Ahuja · Jan 20, 2019 · Viewed 7.7k times · Source

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

from torchvision import datasets

Answer

kmario23 picture kmario23 · Jan 20, 2019

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.