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

Question:

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?

Asked By: Myexgiko

||

Answers:

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

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.

Answered By: Warren Weckesser

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
Answered By: Hamid Rasti

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
Answered By: Francois Kneib

You will have two files namely oldstr.py and misc.py

In misc.py:
change from collections import Mapping to from collections.abc import Mapping

and

In oldstr.py:
change from collections import Iterable to from collections.abc import Iterable

Simply… just add .abc after collections on import statement

Answered By: Favas
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.