英文:
Import error with named tuple and Mapping
问题
I have trying to fix an import error with the requests
library in Python 3.11. This is to run a Discord bot that I have created. My original error is given as ImportError: cannot import name 'Mapping' from 'collections'
and in response I have tried changing a line from from collections import namedtuple, Mapping
to from collections.abc import namedtuple, Mapping
. This has not fixed my original error. The new error created is the traceback given below:
Traceback (most recent call last):
File "E:\python projects\newer Vorpal\main.py", line 4, in <module>
from game import game, shopping
File "E:\python projects\newer Vorpal\game.py", line 10, in <module>
from monsters import createmonsters
File "E:\python projects\newer Vorpal\monsters.py", line 2, in <module>
import requests
File "C:\Users\andreas\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests_init_.py", line 43, in <module>
import urllib3
File "C:\Users\andreas\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3_init_.py", line 8, in <module>
from .connectionpool import (
File "C:\Users\andreas\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 29, in <module>
from .connection import (
File "C:\Users\andreas\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connection.py", line 39, in <module>
from .util.ssl_ import (
File "C:\Users\andreas\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\util_init_.py", line 3, in <module>
from .connection import is_connection_dropped
File "C:\Users\andreas\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\util\connection.py", line 3, in <module>
from .wait import wait_for_read
File "C:\Users\andreas\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\util\wait.py", line 1, in <module>
from .selectors import (
File "C:\Users\andreas\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\util\selectors.py", line 14, in <module>
from collections.abc import namedtuple, Mapping
ImportError: cannot import name 'namedtuple' from 'collections.abc' (C:\Users\andreas\AppData\Local\Programs\Python\Python311\Lib\collections\abc.py)
英文:
I have trying to fix an import error with the requests
library in Python 3.11. This is to run a Discord bot that I have created. My original error is given as ImportError: cannot import name 'Mapping' from 'collections'
and in response I have tried changing a line from from collections import namedtuple, Mapping
to from collections.abc import namedtuple, Mapping
. This has not fixed my original error. The new error created is the traceback given below:
Traceback (most recent call last):
File "E:\python projects\newer Vorpal\main.py", line 4, in <module>
from game import game, shopping
File "E:\python projects\newer Vorpal\game.py", line 10, in <module>
from monsters import createmonsters
File "E:\python projects\newer Vorpal\monsters.py", line 2, in <module>
import requests
File "C:\Users\andreas\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\__init__.py", line 43, in <module>
import urllib3
File "C:\Users\andreas\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\__init__.py", line 8, in <module>
from .connectionpool import (
File "C:\Users\andreas\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 29, in <module>
from .connection import (
File "C:\Users\andreas\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connection.py", line 39, in <module>
from .util.ssl_ import (
File "C:\Users\andreas\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\util\__init__.py", line 3, in <module>
from .connection import is_connection_dropped
File "C:\Users\andreas\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\util\connection.py", line 3, in <module>
from .wait import wait_for_read
File "C:\Users\andreas\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\util\wait.py", line 1, in <module>
from .selectors import (
File "C:\Users\andreas\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\util\selectors.py", line 14, in <module>
from collections.abc import namedtuple, Mapping
ImportError: cannot import name 'namedtuple' from 'collections.abc' (C:\Users\andreas\AppData\Local\Programs\Python\Python311\Lib\collections\abc.py)
答案1
得分: 2
namedtuple
位于collections模块中,Mapping
位于collections.abc模块中。从Python 3.10开始,from collections import Mapping
会引发错误。您应该将这两个导入分开,像这样:
from collections.abc import Mapping
from collections import namedtuple
英文:
namedtuple
is located in the collections module, Mapping
-- in the collections.abc module. Starting from python 3.10 the line from collections import Mapping
throws an error. You should separate the two imports, like so:
from collections.abc import Mapping
from collections import namedtuple
答案2
得分: 2
您必须正在使用已过时的 urllib3
版本。这个问题在 2018 年 2 月得到了修复1,然后在 2018 年 6 月发布的版本 1.23 之前3,完全删除了 selectors
模块。
英文:
You must be using an outdated version of urllib3
. This issue was fixed in Feb 2018 and then the selectors
module was removed entirely, before version 1.23, released in Jun 2018.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论