【问题标题】:cannot import name 'MutableMapping' from 'collections' [duplicate]无法从“集合”中导入名称“MutableMapping”[重复]
【发布时间】:2022-05-09 02:58:33
【问题描述】:

我收到以下错误:

  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)

并且谷歌搜索显示烧瓶必须> = 2.0,所以我做到了

$ sudo pacman -Syu python-flask

安装了哪个版本(2.0.2-3

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

$ python3.10 -m pip install babelfish -U

向我展示了:

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

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

【问题讨论】:

  • 您必须使用from collections.abc import MutableMapping 导入,而不是从from collections import MutableMapping 导入
  • 旁注:使用操作系统升级 Flask 可能在与您使用的 3.10 不同的 Python 版本上运行。这可能是您拥有的唯一版本,但我认为不太可能。

标签: python linux pip package manjaro


【解决方案1】:

需要导入collection.abc

这里是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

自 3.3 版起已弃用,将在 3.10 版中删除:将 Collections Abstract Base Classes 移至 collections.abc 模块。为了向后兼容,它们通过 Python 3.9 继续在此模块中可见。

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

【讨论】:

  • 你建议 OP 给猴子补丁 OpenBMC,我没听错吗?
【解决方案2】:

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

import sys

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

【讨论】:

    【解决方案3】:

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

    from collections.abc import MutableMapping
    

    这是我收到的贬损警告

    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
    

    【讨论】:

      猜你喜欢
      • 2021-12-28
      • 2022-11-10
      • 1970-01-01
      • 2018-10-18
      • 2021-01-01
      • 2019-05-27
      • 2021-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多