无法从“集合”导入名称“MutableMapping”

std*_*err 32 python linux pip package manjaro

我收到以下错误:

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

谷歌搜索显示 Flask 必须 >=2.0,所以我这样做了

\n
$ sudo pacman -Syu python-flask\n
Run Code Online (Sandbox Code Playgroud)\n

安装的版本 ( 2.0.2-3)

\n

这并没有解决问题。进一步搜索发现 babelfish 也需要升级,所以我这样做了:

\n
$ python3.10 -m pip install babelfish -U\n
Run Code Online (Sandbox Code Playgroud)\n

这向我展示了:

\n
Defaulting to user installation because normal site-packages is not writeable\nRequirement already satisfied: babelfish in /home/ron/.local/lib/python3.10/site-packages (0.6.0)\nCollecting babelfish\n  Using cached babelfish-0.6.0-py3-none-any.whl (93 kB)\n  Downloading babelfish-0.5.5.tar.gz (90 kB)\n     |\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88| 90 kB 406 kB/s\n
Run Code Online (Sandbox Code Playgroud)\n

但我仍然遇到同样的错误。谁能告诉我还缺少什么吗?

\n

小智 37

您需要导入collections.abc

这是文档的链接

>>> 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
Run Code Online (Sandbox Code Playgroud)

自版本 3.3 起已弃用,将在版本 3.10 中删除:将集合抽象基类移至collections.abc模块。为了向后兼容,它们在 Python 3.9 中继续在此模块中可见。

参考号 https://docs.python.org/3.9/library/collections.html


nav*_*ver 12

如果您有多名口译员,请使用:

import sys

if sys.version_info[:2] >= (3, 8):
    from collections.abc import MutableMapping
else:
    from collections import MutableMapping
Run Code Online (Sandbox Code Playgroud)


Kab*_*raj 6

自 Python 3.3 起,直接导入已被弃用,并将在 Python 3.9 中停止工作。您需要使用导入

from collections.abc import MutableMapping
Run Code Online (Sandbox Code Playgroud)

这是我收到的贬值警告

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
Run Code Online (Sandbox Code Playgroud)