Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Python 3.10 and later cannot import Sequence from collections

Your error

ImportError: cannot import name 'Sequence' from 'collections'1 more line it can show

Fix it fast

Most common cause, 1 of 3

A module in your project still imports the ABC from collections

Import the abstract classes from collections.abc.

Fix
# before
from collections import Sequence, Mapping, OrderedDict

# after
from collections import OrderedDict
from collections.abc import Sequence, Mapping
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

ImportError: cannot import name 'Sequence' from 'collections' appears on Python 3.10 and later, which removed the abstract base class aliases (Sequence, Mapping, MutableMapping, Iterable, Callable) from collections. They live in collections.abc since 3.3. Change your import to from collections.abc import Sequence, or upgrade the package that still uses the old path. Applies to Python 3.10 through 3.15.

  • Applies to Python 3.10-3.15, pip 26.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Print only the file lines of the traceback. The final one shows who wrote the failing import: a path in your project, or a folder under site-packages that belongs to an installed package.

Run this first
python app.py 2>&1 | grep 'File "'

Then find the line you see in this list and open its fix:

  1. 1A module in your project still imports the ABC from collectionsThe final File line of the traceback is one of your own .py files
  2. 2A pinned old release of a dependency uses the removed importThe final File line is under site-packages/<package>/, and requirements.txt or a lockfile pins that package to an old version
  3. 3The package has no release that fixes the importpip reports the newest version is already installed, yet the error still comes from that package's files

What does the error look like?

  • ImportError: cannot import name 'Sequence' from 'collections'
  • ImportError: cannot import name 'Mapping' from 'collections'

Cause 1 of 3

A module in your project still imports the ABC from collections

You seeThe final File line of the traceback is one of your own .py files

Check

Your code raises cannot import name 'Sequence' from 'collections' because Python 3.10 deleted the old aliases from that module. The classes have lived in collections.abc since 3.3; the old import path was only a compatibility alias. Attribute access such as collections.MutableMapping breaks for the same reason.

Check
grep -rnE "from collections import .*(Sequence|Mapping|Iterable|Callable|Set)|collections\.(Mutable)?(Sequence|Mapping|Iterable|Callable)" --include=*.py .

Fix

Import the abstract classes from collections.abc. Keep concrete helpers such as OrderedDict, defaultdict and namedtuple on the plain collections import.

Fix
# before
from collections import Sequence, Mapping, OrderedDict

# after
from collections import OrderedDict
from collections.abc import Sequence, Mapping

Verify

Run the program again on the same interpreter. It starts without the ImportError, and the grep from the check prints nothing.

Verify
python -c "from collections.abc import Sequence; print(Sequence)"

Confirmed by docs.python.org

Did this fix it?

Cause 2 of 3

A pinned old release of a dependency uses the removed import

You seeThe final File line is under site-packages/<package>/, and requirements.txt or a lockfile pins that package to an old version

Check

The installed package was written before Python 3.10 removed the collections aliases and imports Sequence or Mapping the old way. When a later release of that package imports from collections.abc, raising the pin is the whole repair.

Check
python -m pip show <package> | grep -i ^version
python -m pip install --upgrade --dry-run <package>

Fix

Raise the pin to a current release and reinstall. Check that release's changelog for other breaking changes before you deploy it.

Fix
python -m pip install --upgrade <package>
python -m pip freeze | grep -i <package>

Verify

The traceback no longer points into that package, and the application starts on the same Python version.

Verify
python app.py

Confirmed by docs.python.org

Did this fix it?

Cause 3 of 3

The package has no release that fixes the import

You seepip reports the newest version is already installed, yet the error still comes from that package's files

Check

Some abandoned packages never moved to collections.abc, so no upgrade exists. On any Python from 3.10 on, the package cannot be imported as published.

Check
grep -rn "from collections import" "$(python -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])')/<package>"

Fix

Replace the package with a maintained alternative. If you must keep it for now, fork it, change the import to collections.abc, and install your fork from its Git URL.

Fix
# in your fork, inside the package source
sed -i.bak 's/from collections import Mapping/from collections.abc import Mapping/' <package>/module.py
python -m pip install "<package> @ git+https://github.com/<you>/<package>.git"

Verify

Importing the package succeeds on the current Python, and the first-step command shows no frames from it.

Verify
python -c "import <package>"

Confirmed by docs.python.org

Did this fix it?

How do I stop it coming back?

  • Run the test suite under the newest Python in CI as an extra job, so removals show up before a server or base image moves to it.
  • Pin the Python minor version in Docker image tags and CI settings, so a new release never arrives unannounced with a rebuild.
  • Read the Removed section of What's New before moving servers or base images to a new Python minor, and grep the project for each removed name.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.