ImportError:无法使用 Python 3.10 从“collections”导入名称“...”
- 2025-02-25 09:07:00
- admin 原创
- 34
问题描述:
我正在尝试运行使用各种依赖项的程序,但自从升级到 Python 3.10 后,它就不再起作用了。当我在终端中运行“python3”并从那里导入我的依赖项时,我收到一个错误:
ImportError: cannot import name 'Mapping' from 'collections' (/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/collections/__init__.py)
这似乎是一个普遍的问题,但是这是我的具体案例的回溯:
Traceback (most recent call last):
File "/Users/mk/Flasktut/app.py", line 2, in <module>
from flask import Flask, render_template
File "/Users/mk/Flasktut/env/lib/python3.10/site-packages/flask/__init__.py", line 14, in <module>
from jinja2 import escape
File "/Users/mk/Flasktut/env/lib/python3.10/site-packages/jinja2/__init__.py", line 33, in <module>
from jinja2.environment import Environment, Template
File "/Users/mk/Flasktut/env/lib/python3.10/site-packages/jinja2/environment.py", line 16, in <module>
from jinja2.defaults import BLOCK_START_STRING, \n File "/Users/mk/Flasktut/env/lib/python3.10/site-packages/jinja2/defaults.py", line 32, in <module>
from jinja2.tests import TESTS as DEFAULT_TESTS
File "/Users/mk/Flasktut/env/lib/python3.10/site-packages/jinja2/tests.py", line 13, in <module>
from collections import Mapping
ImportError: cannot import name 'Mapping' from 'collections' (/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/collections/__init__.py)
解决方案 1:
改变:
from collections import Mapping
到
from collections.abc import Mapping
解决方案 2:
这是由于从 Python 3.10 开始界面发生变化而导致的collections
。据我所知,目前有三种方法可以缓解此问题:
如果错误发生在第三方库中,请先尝试更新此库(
pip install <package> --upgrade
)。恢复到 Python 3.9。
手动修补代码。
有关修补的信息ImportError
,请参阅https://stackoverflow.com/a/69727802/13994294。
解决方案 3:
对于 Python 的更高版本,您需要从abc
里面的新模块导入。collections
如果你需要让你的代码向后兼容旧版本的 Python,你可以使用以下命令:
try:
from collections.abc import Mapping
except ImportError:
from collections import Mapping
解决方案 4:
错误原因
如果你尝试在 Python 3.9.x 中导入,就会清楚:
Python 3.9.10 (main, Jan 15 2022, 11:40:53)
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from collections import Mapping
<stdin>:1: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.10 it will stop working
它明确提到在 3.10 中它将停止工作。因此要使用,请将版本更改为 Python 3.9 或更低版本。如果您使用pipenv来管理虚拟环境,则步骤如下:
$ pipenv --rm # to remove the virtual env
$ exit # to exit the virtual env
$ vim Pipfile # here change the version to '3.9' by replacing '3.10'
$ pipenv shell # this will create a virtual env with 3.9
$ pipenv install # to install the requirements
我们刚刚从 Python 3.10 切换到 Python 3.9,它目前支持代码。
解决方案 5:
使用旧版本的python(例如3.8)
解决方案 6:
我补充道
from collections.abc import Mapping
from collections.abc import MutableMapping
from collections.abc import Sequence
在
“C:\Users\natha\AppData\Local\Programs\Python\Python311\Lib\collections_ init _.py”
解决方案 7:
导致此问题的原因可能是软件包过时
就我而言,将 requirements.txt 上的PyJWT更新到最新版本解决了这个问题
解决方案 8:
这是关于 Python 版本的问题。大多数时候 Python 3.10 都会出现这个问题。
您可以使用 python 3.9 或 3.8 版本来解决此问题。或者如果来自 python-docx 等软件包或其他有关 MS 的软件包的错误,您可能可以使用 pipwin 来解决它。
解决方案 9:
同样如此,但我通过以下方式解决了这个问题:
pip3 list --outdated --format=freeze | grep -v '^-e' | cut -d = -f 1 | xargs -n1 pip3 install -U
这
解决方案 10:
建议在虚拟环境中使用以前版本的 Python(例如 3.8 或 3.9),而不是在根系统上恢复 Python。
virtualenv --python="/YOUR PATH/python3.9" "name of your env"
解决方案 11:
在我的环境中,使用错误修复 Python 版本 3.10.2 解决了这个问题
解决方案 12:
只需更新到请求 2.27.1和python 3.10.2或更高版本,问题就会得到解决。