Python 3.10 遇到 ImportError: cannot import name ‘Mapping’ from ‘collections 问题 - V2EX
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 2598 次点击
    这是一个创建于 1163 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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     Solana     6131 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 29ms UTC 03:10 PVG 11:10 LAX 20:10 JFK 23:10
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86