'ImportError: cannot import name 'Iterable' from 'collections' in Python

Working in Python with Atom on a Mac. Code:

from rubik.cube import Cube
from rubik_solver import utils

Full error:

Traceback (most recent call last):
  File "/Users/Audey/Desktop/solver.py", line 2, in <module>
    from rubik_solver import utils
  File "/Users/Audey/Library/Python/3.10/lib/python/site-packages/rubik_solver/utils.py", line 4, in <module>
    from past.builtins import basestring
  File "/Users/Audey/Library/Python/3.10/lib/python/site-packages/past/builtins/__init__.py", line 43, in <module>
    from past.builtins.noniterators import (filter, map, range, reduce, zip)
  File "/Users/Audey/Library/Python/3.10/lib/python/site-packages/past/builtins/noniterators.py", line 24, in <module>
    from past.types import basestring
  File "/Users/Audey/Library/Python/3.10/lib/python/site-packages/past/types/__init__.py", line 25, in <module>
    from .oldstr import oldstr
  File "/Users/Audey/Library/Python/3.10/lib/python/site-packages/past/types/oldstr.py", line 5, in <module>
    from collections import Iterable
ImportError: cannot import name 'Iterable' from 'collections' (/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/collections/__init__.py)

The from rubik_solver import utils is what is causing the error as when I remove it the error does not appear. I am not sure what is causing the error and hove checked there code and found it on other sources so am sure that it should work. Any solves?



Solution 1:[1]

The Iterable abstract class was removed from collections in Python 3.10. See the deprecation note in the 3.9 collections docs. In the section Removed of the 3.10 docs, the item

  • Remove deprecated aliases to Collections Abstract Base Classes from the collections module. (Contributed by Victor Stinner in bpo-37324.)

is what results in your error.

You can use Iterable from collections.abc instead, or use Python 3.9 if the problem is in a dependency that can't be updated.

Solution 2:[2]

As said in other answers, the issue is the deprecation of some aliases from collections.abc into collections from python 3.10.

If you can't modify the importations in your scripts because of a third-party import, as a temporary workaround you can do the aliases manually before importing the problematic third-party lib. See the example below for the importation of hyper library for example, which causes similar issues as yours.

import collections.abc
#hyper needs the four following aliases to be done manually.
collections.Iterable = collections.abc.Iterable
collections.Mapping = collections.abc.Mapping
collections.MutableSet = collections.abc.MutableSet
collections.MutableMapping = collections.abc.MutableMapping
#Now import hyper
import hyper

Solution 3:[3]

In python version 3.10 you should import Iterable from collections.abc instead:

from collections.abc import Iterable

or you can:

try:
    from collections.abc import Iterable
except ImportError:
    from collections import Iterable

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Francois Kneib
Solution 3 hamraa