V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
ggvm
V2EX  ›  Python

Python 3.10 遇到 ImportError: cannot import name ‘Mapping’ from ‘collections 问题

  •  
  •   ggvm · 2022-08-04 15:36:38 +08:00 · 2034 次点击
    这是一个创建于 630 天前的主题,其中的信息可能已经有所发展或是发生改变。
    Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
    Type “help”, “copyright”, “credits” or “license” for more information.

    import jwt
    Traceback (most recent call last):
    File “”, line 1, in
    File “C:\Python310\lib\site-packages\jwt__init__.py”, line 19, in
    from .api_jwt import (
    File “C:\Python310\lib\site-packages\jwt\api_jwt.py”, line 5, in
    from collections import Mapping
    ImportError: cannot import name ‘Mapping’ from ‘collections’ (C:\Python310\lib\collections__init__.py)

    在安装 PyJWT = 1.40 来处理 flask 使用 jwt 的问题时候,由于 python 3.10 的版本相对较高,所以会遇到上面的问题:from collections import Mapping
    ImportError: cannot import name ‘Mapping’ from ‘collections’



    遇到这个问题的时候,建议不要修改 collections 的代码,修改 pyJWT 的更加可控

    这个时候,只需要把 PyJWT 相关的代码修改一下:

    from collections import Mapping
    修改为

    from collections.abc import Mapping

    https://zhuji188.com/499.html
    4 条回复    2022-08-05 10:06:24 +08:00
    frostming
        1
    frostming  
       2022-08-04 17:51:12 +08:00
    pyjwt 最新版都 2.4 了,升级版本不就好了
    tomczhen
        2
    tomczhen  
       2022-08-04 17:53:05 +08:00 via Android   ❤️ 1
    谜一样的逻辑,Python 版本我就要最新,包版本升级跟要命一样。
    ggvm
        3
    ggvm  
    OP
       2022-08-04 17:56:16 +08:00
    @frostming 神奇的是升级到最高版本也没用。
    ysc3839
        4
    ysc3839  
       2022-08-05 10:06:24 +08:00 via Android
    https://docs.python.org/3.7/library/collections.html
    Mapping 自从 Python 3.3 被移动到 collections.abc ,并在 3.7 宣布将在 3.9 移除。
    https://github.com/jpadilla/pyjwt/blob/2.0.0a1/jwt/api_jwt.py
    pyjwt 2.0.0a1 就已经改用 collections.abc 了,你确定升级到最新的还是有问题吗?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2766 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 15:42 · PVG 23:42 · LAX 08:42 · JFK 11:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.