24

I'm getting the following error:

  File "/home/ron/rzg2l_bsp_v1.3/poky/bitbake/lib/bb/compat.py", line 7, in <module>
    from collections import MutableMapping, KeysView, ValuesView, ItemsView, OrderedDict
ImportError: cannot import name 'MutableMapping' from 'collections' (/usr/lib/python3.10/collections/__init__.py)

and Googling revealed that flask has to be >=2.0, so I did

$ sudo pacman -Syu python-flask

which installed version (2.0.2-3)

which did not resolve the issue. Further searching revealed that babelfish needs to be upgraded too, so I did:

$ python3.10 -m pip install babelfish -U

which showed me:

Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: babelfish in /home/ron/.local/lib/python3.10/site-packages (0.6.0)
Collecting babelfish
  Using cached babelfish-0.6.0-py3-none-any.whl (93 kB)
  Downloading babelfish-0.5.5.tar.gz (90 kB)
     |████████████████████████████████| 90 kB 406 kB/s

but I'm still getting the same error. Can anyone tell what else I'm missing?

stdcerr
  • 13,725
  • 25
  • 71
  • 128
  • 2
    You have to import using `from collections.abc import MutableMapping` instead from `from collections import MutableMapping` – Kabilan Mohanraj Jan 26 '22 at 20:49
  • 2
    side note: using the OS to upgrade Flask might be operating on a different version of Python than the 3.10 you're using. that might be the only version you have, but i think unlikely. – mechanical_meat Jan 26 '22 at 20:51
  • 1
    https://stackoverflow.com/search?q=ImportError%3A+cannot+import+name+MutableMapping+from+collections – phd Jan 26 '22 at 21:57
  • If you have this issue a simple method to patch the issue for 3.10 is to port back the library into collections instead of bothering about changing third party code, or your own code to be compliant with the latest python fad from python community. The code is roughly -- """import collections from collections.abc import MutableMapping #or add python version check if(not hasattr(collections,"MutableMapping"): collections.MutableMapping = MutableMapping """ – user6830669 Aug 19 '22 at 08:44

3 Answers3

30

You need to import collections.abc

Here the link to doc

>>> from collections import MutableMapping
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'MutableMapping' from 'collections' (/usr/lib/python3.10/collections/__init__.py)
>>> from collections.abc import MutableMapping

Deprecated since version 3.3, will be removed in version 3.10: Moved Collections Abstract Base Classes to the collections.abc module. For backwards compatibility, they continue to be visible in this module through Python 3.9.

Ref. https://docs.python.org/3.9/library/collections.html

fjarlq
  • 2,419
  • 1
  • 16
  • 10
Nabil
  • 1,130
  • 5
  • 11
  • You advise OP to monkey-patch [`OpenBMC`](https://gerrit.openbmc-project.xyz/plugins/gitiles/openbmc/openbmc/+/d1a90aa35d35426789d8f4061166a6dd8d27a30e/poky/bitbake/lib/bb/compat.py), did I got you right? – Yevgeniy Kosmak Jan 26 '22 at 20:58
  • 1
    @YevgeniyKosmak well, yep. That's what I did, at least. I only needed to run a given software just once and that monkey-patching was way easier to do than installing a Python version prior to 3.10. – Luis Milanese May 26 '22 at 20:51
  • I am using Python 3.10.6 and that did not work for me. > ImportError: cannot import name 'MutableMapping' from 'collections' > (/usr/local/lib/python3.10/collections/init.py) For me Modules Mapping, MutableMapping and Sequence were missing in the **/usr/local/lib/python3.10/collections/init.py** adding them solved the problem. `from _collections_abc import Mapping` `from _collections_abc import MutableMapping` `from _collections_abc import Sequence` – Namwanza Ronald Nov 10 '22 at 08:48
10

If you have more than one interpreter, use:

import sys

if sys.version_info[:2] >= (3, 8):
    from collections.abc import MutableMapping
else:
    from collections import MutableMapping
navylover
  • 12,383
  • 5
  • 28
  • 41
6

The direct import has been deprecated since Python 3.3 and will stop working in Python 3.9. You need to import using

from collections.abc import MutableMapping

This is the deprication warning I got

DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3,and in 3.9 it will stop working
Kabilan Mohanraj
  • 1,856
  • 1
  • 7
  • 17