导入命名元组和映射时出现错误

huangapple go评论67阅读模式
英文:

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 &#39;Mapping&#39; from &#39;collections&#39; 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 &quot;E:\python projects\newer Vorpal\main.py&quot;, line 4, in &lt;module&gt;
from game import game, shopping
  File &quot;E:\python projects\newer Vorpal\game.py&quot;, line 10, in &lt;module&gt;
from monsters import createmonsters
  File &quot;E:\python projects\newer Vorpal\monsters.py&quot;, line 2, in &lt;module&gt;
import requests
  File &quot;C:\Users\andreas\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\__init__.py&quot;, line 43, in &lt;module&gt;
import urllib3
  File &quot;C:\Users\andreas\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\__init__.py&quot;, line 8, in &lt;module&gt;
from .connectionpool import (
  File &quot;C:\Users\andreas\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py&quot;, line 29, in &lt;module&gt;
from .connection import (
  File &quot;C:\Users\andreas\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connection.py&quot;, line 39, in &lt;module&gt;
from .util.ssl_ import (
  File &quot;C:\Users\andreas\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\util\__init__.py&quot;, line 3, in &lt;module&gt;
from .connection import is_connection_dropped
  File &quot;C:\Users\andreas\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\util\connection.py&quot;, line 3, in &lt;module&gt;
from .wait import wait_for_read
  File &quot;C:\Users\andreas\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\util\wait.py&quot;, line 1, in &lt;module&gt;
from .selectors import (
  File &quot;C:\Users\andreas\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\util\selectors.py&quot;, line 14, in &lt;module&gt;
from collections.abc import namedtuple, Mapping

ImportError: cannot import name &#39;namedtuple&#39; from &#39;collections.abc&#39; (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.

huangapple
  • 本文由 发表于 2023年5月11日 02:35:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定