Home > other >  ImportError: cannot import name 'Iterable' from 'collections' in Python
ImportError: cannot import name 'Iterable' from 'collections' in Python

Time:04-28

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?

CodePudding user response:

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

CodePudding user response:

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.

  • Related