Skip to content
Latchkey

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.

python
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

  1. Change your own imports to from collections.abc import Mapping.
  2. Re-run on the 3.10+ runner.
Python
from collections.abc import Mapping, Sequence, Iterable

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

Terminal
pip install --upgrade the-old-lib   # to a 3.10+ compatible release

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

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →