partially initialized module 'torchvision' has no attribute 'extension' (fix)

AttributeError: partially initialized module 'torchvision' has no attribute 'extension' (most likely due to a circular import) is usually not a circular import. It is what the second import torchvision prints after the first one failed. We reproduced it and show how to find the real error.

1. Reproduce it

This script imports torchvision twice, the way re-running a notebook cell or a library's try/except around an import does:

import torch
print("torch", torch.__version__)
for attempt in (1, 2):
    try:
        import torchvision
        print("attempt", attempt, "OK", torchvision.__version__)
    except Exception as e:
        print("attempt", attempt, type(e).__name__ + ":", e)

torch 2.8.0 with torchvision 0.24.0 (a mismatched pair):

torch 2.8.0+cpu
attempt 1 RuntimeError: operator torchvision::nms does not exist
attempt 2 AttributeError: partially initialized module 'torchvision' has no attribute 'extension' (most likely due to a circular import)

The first import fails with the real error. Python then drops torchvision from sys.modules but keeps the submodules that already loaded, including torchvision.extension. On the second import those submodules come from the cache, so they are never attached to the new torchvision module object, and torchvision/_meta_registrations.py fails on torchvision.extension._has_ops(). The "circular import" hint is misleading here.

2. Find the first error

Restart the kernel or process and import torchvision once, with nothing catching the exception. In a notebook: Restart, then run import torchvision in the first cell. Whatever that prints is what needs fixing. Most often it is operator torchvision::nms does not exist (torch and torchvision from different releases or builds), as here.

3. Fix the first error

For the mismatch case, install a matching pair from one index in one command, e.g. pip install torch==2.9.0 torchvision==0.24.0 --index-url https://download.pytorch.org/whl/cpu (or your CUDA tag). Same script on torch 2.9.0 with torchvision 0.24.0:

torch 2.9.0+cpu
attempt 1 OK 0.24.0+cpu
attempt 2 OK 0.24.0+cpu

Also check that no file or folder named torchvision or torch sits in your working directory, which causes a true circular import.

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

Frequently asked questions

Why does it say circular import?

Python adds that hint to any AttributeError on a module that is still being imported. Here the cause is a failed earlier import whose submodules stayed cached.

Why does restarting change the error?

A fresh process imports torchvision once, so you see the first, real error (for example operator torchvision::nms does not exist) instead of this one.

Written with AI assistance. Measured on 2026-10-24 on Linux, CPython 3.11.17: torch 2.8.0+cpu with torchvision 0.24.0+cpu, and torch 2.9.0+cpu with torchvision 0.24.0+cpu. Output pasted unedited. PlainTables is our extension; torchvision is free.