Fix AttributeError: module ‘torchvision’ has no attribute ‘__version__’ – PyTorch Tutorial

By | December 7, 2021

When we are running a pytorch script, we get AttributeError: module ‘torchvision’ has no attribute ‘__version__’. In this tutorial, we will introduce how to fix it.

This error looks like:

Fix AttributeError - module 'torchvision' has no attribute '__version__' - PyTorch Tutorial

Why this AttributeError occur?

Becuase the version of pytorch and torchvision is incompatible.

Check the pytorch version

We can use code below to the pytorch version of we have installed.

>>> import torch
>>> print(torch.__version__)
1.10.0+cu102

We can find our version is 1.10.0

We can find the torchvision version we should install from here:

As to us, we will install torchvision 0.11.1

install torchvision based on pytorch version

Check the torchvision version

We can find the version of torchvision as follows:

import torchvision
>>> torchvision.__version__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'torchvision' has no attribute '__version__'
>>> torchvision.__path
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'torchvision' has no attribute '__path'
>>> torchvision.__path__
['/home/appadmin/anaconda3/lib/python3.6/site-packages/torchvision']

Then, we will open path:/home/appadmin/anaconda3/lib/python3.6/site-packages/torchvision

torchvision 0.4.2 version

We can find our torchvision version is 0.4.2, whis is not 0.11.1

We will unstall it and install 0.11.1 version.

Uninstall torchvision

We use pip to uninstall it.

pip uninstall torchvision

You will see:

pip uninstall torchvision

Install torchvision

We will install torchvision 0.11.1

pip install torchvision==0.11.1

Then we will find:

pip install torchvision

Check this error is fixed or not

Run code below:

mport torchvision
>>> torchvision.__version__
'0.11.1+cu102'

We will find this error is fixed.