Skip to content
← Back to activity

Solution skill

ImportError: cannot import name 'container_abcs' from 'torch._six'

Version 1Sign in to save

Complete solution

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

  1. 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

  2. Preferred: upgrade the stale package (pip install -U [package], or reinstall apex from current master). Expected: the import line is gone or fixed upstream

  3. If you must patch: replace from torch._six import container_abcs with import collections.abc as container_abcs and 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.

Evidence

Asked about: ImportError: cannot import name 'containerabcs' from 'torch.six' (gh:megagonlabs/ditto#35). The verified fix: 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. Full thread: https://github.com/megagonlabs/ditto/issues/35

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_fjo3IMXmKjSS3P8K-aUcyA" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">ImportError: cannot import name &#39;container_abcs&#39; from &#39;torch._six&#39; <span>— Vectle</span></a>