0

I get this error in Python3.10.6

    isinstance(pattern, collections.Iterable):
AttributeError: module 'collections' has no attribute 'Iterable'

in a significant and critical legacy program I didn't write (it's the buildsystem for another product). It worked fine with 3.8.10. The error is breaking the build on GitHub CI under Linux. Seems like they upgraded Python a couple of days ago. Does anyone have a suggestion to fix it?

Yttrill
  • 4,725
  • 1
  • 20
  • 29
  • 1
    https://stackoverflow.com/questions/59809785/i-get-a-attributeerror-module-collections-has-no-attribute-iterable-when-i – Bijay Regmi Dec 11 '22 at 10:55
  • `collections.abc.Iterable` – Bijay Regmi Dec 11 '22 at 10:55
  • ok thanks I think i get the problem, the code has to work on 3.8 AND 3.10, so just adding `abc` isn't going to work. There are many files in the program containing `import collections` and that still works. Maybe `import collections.abc` and fallback to `import collections` if that fails? – Yttrill Dec 11 '22 at 11:08
  • ```try: collectionsIterable = collections.Iterable except: collectionsIterable = collections.abc.Iterable ``` does this on 3.10 now `AttributeError: module 'collections' has no attribute 'abc'` but it does work on 3.8 – Yttrill Dec 11 '22 at 11:30

2 Answers2

0

I tried following code in python3.10 and python3.8 and it seems to work on both:

try:
    # python 3.10
    from collections.abc import Iterable
except ImportError:
    # if failed, fall back to python 3.8
    from collections import Iterable


pattern = [1, 2, 3]

print(isinstance(pattern, Iterable))

results :

True
Bijay Regmi
  • 1,187
  • 2
  • 11
  • 25
0

collections.abc.Iterable works on both 3.8 and 3.10

Yttrill
  • 4,725
  • 1
  • 20
  • 29