Python "cannot import name 'Mapping' from collections" in CI
The abstract base classes (Mapping, Sequence, Iterable, ...) were importable from collections until 3.9 with a deprecation; 3.10 removed the alias entirely. Code or an old dependency that imports them from collections fails.
What this error means
On a Python 3.10+ runner an import fails with "ImportError: cannot import name 'Mapping' from 'collections'," often from a transitive dependency, not your own code.
Traceback (most recent call last):
File ".../oldlib/utils.py", line 3, in <module>
from collections import Mapping
ImportError: cannot import name 'Mapping' from 'collections'Common causes
ABCs imported from collections on 3.10+
They now live in collections.abc; the legacy alias in collections was dropped in 3.10.
An unmaintained dependency uses the old path
A transitive package never updated its imports and breaks on the newer interpreter.
How to fix it
Import from collections.abc
- Change your own imports to
from collections.abc import Mapping. - Re-run on the 3.10+ runner.
from collections.abc import Mapping, Sequence, IterableUpgrade or pin the offending dependency
When the bad import is in a dependency, upgrade to a version that supports 3.10+, or pin the runner Python below 3.10 if you cannot.
pip install --upgrade the-old-lib # to a 3.10+ compatible releaseHow to prevent it
- Always import ABCs from
collections.abc. - Keep dependencies current with your runner Python version.
- Run the suite against the target Python major/minor before merging.