ImportError: cannot import name 'container_abcs' from 'torch._six'
Fixes ImportError: cannot import name 'container_abcs' from 'torch._six' Use when the traceback shows `from torch._six import container_abcs` failing on torch 1.9+ Not for: torch itself fails to import (DLL/shared-library errors) - that is an install problem, not this
How to fix ImportError: cannot import name 'containerabcs' from 'torch.six'
The library importing torch.six is too old for your torch. `torch.six.containerabcs was removed in torch 1.9; the modern equivalent is collections.abc. Upgrade the offending package (apex, pytorch-lightning, mmcv), or patch its import to import collections.abc as containerabcs`. Your torch is fine; the caller is stale.
The exact error
ImportError: cannot import name 'container_abcs' from 'torch._six'Fix it
- Read the traceback: find which file does
from torch._six import container_abcs(often apex/amp/ampstate.py or pytorch_lightning).
Expected: you have the stale file path
- Preferred: upgrade the stale package (
pip install -U [package], or reinstall apex from current master).
Expected: the import line is gone or fixed upstream
- If you must patch: replace
from torch._six import container_abcswithimport collections.abc as container_abcsand reinstall that package.
Expected: import works on torch>=1.9
When this applies
the traceback shows from torch._six import container_abcs failing on torch 1.9+
When this does NOT apply
torch itself fails to import (DLL/shared-library errors) - that is an install problem, not this
Versions
torch >= 1.9 with old apex / pytorch-lightning / mmcv
Why it happens
torch removed the private _six compat module in 1.9; downstream libs pinned to the old private API.
Edge cases
Downgrading torch below 1.9 'fixes' it too, but breaks everything else modern; upgrade the caller instead.
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.