module 'torchvision.transforms' has no attribute 'v2' (fix)

AttributeError: module 'torchvision.transforms' has no attribute 'v2' has two causes, and we reproduced both: on a current torchvision you used T.v2 without importing the submodule, or your torchvision is too old to have v2 at all.

1. Cause 1: v2 is a submodule you have to import

On a current torchvision, import torchvision.transforms as T does not load T.v2. Using it as an attribute fails until something imports the submodule:

import torchvision
print(torchvision.__version__)
import torchvision.transforms as T
try:
    T.v2
except AttributeError as e:
    print("AttributeError:", e)
import torchvision.transforms.v2
print("after import:", T.v2.Resize)

torchvision 0.24.0:

0.24.0+cpu
AttributeError: module 'torchvision.transforms' has no attribute 'v2'
after import: <class 'torchvision.transforms.v2._geometry.Resize'>

Fix: import it explicitly, either import torchvision.transforms.v2 or (the form the docs use) from torchvision.transforms import v2.

2. Cause 2: your torchvision is too old to have v2

The same script on torchvision 0.14.1 (torch 1.13.1) gets past the attribute error and then fails at the import, because transforms.v2 doesn't exist there:

0.14.1+cu117
AttributeError: module 'torchvision.transforms' has no attribute 'v2'
Traceback (most recent call last):
  File "b2.py", line 8, in <module>
    import torchvision.transforms.v2
ModuleNotFoundError: No module named 'torchvision.transforms.v2'

And the docs' import form:

import torchvision
print(torchvision.__version__)
from torchvision.transforms import v2

torchvision 0.14.1:

0.14.1+cu117
Traceback (most recent call last):
  File "b1.py", line 3, in <module>
    from torchvision.transforms import v2
ImportError: cannot import name 'v2' from 'torchvision.transforms' (.../site-packages/torchvision/transforms/__init__.py)

torchvision 0.24.0:

0.24.0+cpu

Fix: upgrade torchvision together with torch, since each torchvision release needs one exact torch version (0.24 needs torch 2.9.0). Upgrading torchvision alone can leave a mismatched pair that fails with operator torchvision::nms does not exist. If you can't upgrade, use the v1 classes in torchvision.transforms (Resize, ToTensor, ...), which both versions have.

Check which case you are in with python -c "import torchvision; print(torchvision.__version__)".

Related: operator torchvision::nms does not exist ยท torchaudio libtorchaudio.so undefined symbol

Frequently asked questions

Why does transforms.v2 work in the docs but not in my code?

The docs use from torchvision.transforms import v2. import torchvision.transforms as T alone does not load T.v2 (checked on torchvision 0.24.0).

Does torchvision 0.14 have transforms.v2?

No. On 0.14.1 from torchvision.transforms import v2 raises ImportError. Upgrade torch and torchvision together.

Written with AI assistance. Measured on 2026-10-24 on Linux: torchvision 0.24.0+cpu with torch 2.9.0 (CPython 3.11.17) and torchvision 0.14.1 with torch 1.13.1 (CPython 3.10). Output pasted unedited except shortened paths. PlainTables is our extension; torchvision is free.