AttributeError:模块“pkgutil”没有属性“ImpImporter”。您指的是: 'zipimporter'?

sre*_*j t 68 python pip python-3.x

早些时候,我安装了一些软件包,如MatplotlibNumPy、 pip (版本 23.3.1)、wheel (版本 0.41.2)等,并用它们进行了一些编程。我使用该命令C:\\Users\\UserName>pip list查找已安装的软件包列表,并且我使用的是 Python 3.12.0(通过使用 code C:\\Users\\UserName>py -V)。

\n

我需要使用pyspedas来分析一些数据。我按照从站点收到的说明安装该软件包,但有一些变化(我不确定这是否重要:我正在使用py, 而不是python)。我使用的命令(按顺序)是:

\n
py -m venv pyspedas\n.\\pyspedas\\Scripts\\activate\npip install pyspedas\n
Run Code Online (Sandbox Code Playgroud)\n

最后一步之后,我得到以下输出:

\n
Collecting pyspedas\n  Using cached pyspedas-1.4.47-py3-none-any.whl.metadata (14 kB)\nCollecting numpy>=1.19.5 (from pyspedas)\n  Using cached numpy-1.26.1-cp312-cp312-win_amd64.whl.metadata (61 kB)\nCollecting requests (from pyspedas)\n  Using cached requests-2.31.0-py3-none-any.whl.metadata (4.6 kB)\nCollecting geopack>=1.0.10 (from pyspedas)\n  Using cached geopack-1.0.10-py3-none-any.whl (114 kB)\nCollecting cdflib<1.0.0 (from pyspedas)\n  Using cached cdflib-0.4.9-py3-none-any.whl (72 kB)\nCollecting cdasws>=1.7.24 (from pyspedas)\n  Using cached cdasws-1.7.43.tar.gz (21 kB)\n  Installing build dependencies ... done\n  Getting requirements to build wheel ... done\n  Preparing metadata (pyproject.toml) ... done\nCollecting netCDF4>=1.6.2 (from pyspedas)\n  Using cached netCDF4-1.6.5-cp312-cp312-win_amd64.whl.metadata (1.8 kB)\nCollecting pywavelets (from pyspedas)\n  Using cached PyWavelets-1.4.1.tar.gz (4.6 MB)\n  Installing build dependencies ... done\n  Getting requirements to build wheel ... error\n  error: subprocess-exited-with-error\n\n  \xc3\x97 Getting requirements to build wheel did not run successfully.\n  \xe2\x94\x82 exit code: 1\n  \xe2\x95\xb0\xe2\x94\x80> [33 lines of output]\n      Traceback (most recent call last):\n        File "C:\\Users\\UserName\\pyspedas\\Lib\\site-packages\\pip\\_vendor\\pyproject_hooks\\_in_process\\_in_process.py", line 353, in <module>\n          main()\n        File "C:\\Users\\UserName\\pyspedas\\Lib\\site-packages\\pip\\_vendor\\pyproject_hooks\\_in_process\\_in_process.py", line 335, in main\n          json_out[\'return_val\'] = hook(**hook_input[\'kwargs\'])\n                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n        File "C:\\Users\\UserName\\pyspedas\\Lib\\site-packages\\pip\\_vendor\\pyproject_hooks\\_in_process\\_in_process.py", line 112, in get_requires_for_build_wheel\n          backend = _build_backend()\n                    ^^^^^^^^^^^^^^^^\n        File "C:\\Users\\UserName\\pyspedas\\Lib\\site-packages\\pip\\_vendor\\pyproject_hooks\\_in_process\\_in_process.py", line 77, in _build_backend\n          obj = import_module(mod_path)\n                ^^^^^^^^^^^^^^^^^^^^^^^\n        File "C:\\Users\\UserName\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\importlib\\__init__.py", line 90, in import_module\n          return _bootstrap._gcd_import(name[level:], package, level)\n                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n        File "<frozen importlib._bootstrap>", line 1381, in _gcd_import\n        File "<frozen importlib._bootstrap>", line 1354, in _find_and_load\n        File "<frozen importlib._bootstrap>", line 1304, in _find_and_load_unlocked\n        File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed\n        File "<frozen importlib._bootstrap>", line 1381, in _gcd_import\n        File "<frozen importlib._bootstrap>", line 1354, in _find_and_load\n        File "<frozen importlib._bootstrap>", line 1325, in _find_and_load_unlocked\n        File "<frozen importlib._bootstrap>", line 929, in _load_unlocked\n        File "<frozen importlib._bootstrap_external>", line 994, in exec_module\n        File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed\n        File "C:\\Users\\UserName\\AppData\\Local\\Temp\\pip-build-env-_lgbq70y\\overlay\\Lib\\site-packages\\setuptools\\__init__.py", line 16, in <module>\n          import setuptools.version\n        File "C:\\Users\\UserName\\AppData\\Local\\Temp\\pip-build-env-_lgbq70y\\overlay\\Lib\\site-packages\\setuptools\\version.py", line 1, in <module>\n          import pkg_resources\n        File "C:\\Users\\UserName\\AppData\\Local\\Temp\\pip-build-env-_lgbq70y\\overlay\\Lib\\site-packages\\pkg_resources\\__init__.py", line 2191, in <module>\n          register_finder(pkgutil.ImpImporter, find_on_path)\n                          ^^^^^^^^^^^^^^^^^^^\n      AttributeError: module \'pkgutil\' has no attribute \'ImpImporter\'. Did you mean: \'zipimporter\'?\n      [end of output]\n\n  note: This error originates from a subprocess, and is likely not a problem with pip.\nerror: subprocess-exited-with-error\n\n\xc3\x97 Getting requirements to build wheel did not run successfully.\n\xe2\x94\x82 exit code: 1\n\xe2\x95\xb0\xe2\x94\x80> See above for output.\n\nnote: This error originates from a subprocess, and is likely not a problem with pip.\n
Run Code Online (Sandbox Code Playgroud)\n

经过一番谷歌搜索后,我发现多个地方都报告了这个问题,但这个包没有报告这个问题。我确实按照此处的答案中所述在新环境中安装了wheel ,但问题仍然存在。

\n

我没有设置虚拟环境,而是简单地执行了命令py -m pip install pyspedas。但我仍然收到错误。

\n

我能收集到的是该程序有问题

\n
Collecting pywavelets (from pyspedas)\n  Using cached PyWavelets-1.4.1.tar.gz (4.6 MB)\n  Installing build dependencies ... done\n
Run Code Online (Sandbox Code Playgroud)\n

我在 Windows 11 中使用IDLE 。

\n

Pal*_*ine 84

如果您可以降级到 Python 3.11,那么您可能不会遇到任何问题。


如果您必须仅使用 Python 3.12,那么这里是解决该问题的尝试:


AttributeError:模块“pkgutil”没有属性“ImpImporter”。您指的是: 'zipimporter'?

由于使用 Python 3.12 而发生。

由于删除了长期弃用的 pkgutil.ImpImporter 类,pip 命令可能不适用于 Python 3.12。

以下是描述它的帖子的链接:https://ubuntuhandbook.org/index.php/2023/10/fix-broken-pip-python-312-ubuntu/

您只需手动安装 Python 3.12 的 pip

有几种方法可以解决这个问题。在您的虚拟环境中:

pip install --upgrade setuptools
Run Code Online (Sandbox Code Playgroud)

任务1:

Python自带了一个ensurepip,可以在Python环境中安装pip。

https://pip.pypa.io/en/stable/installation/

在Linux/macOS终端上:

python -m ensurepip --upgrade
Run Code Online (Sandbox Code Playgroud)

在Windows上:

py -m ensurepip --upgrade
Run Code Online (Sandbox Code Playgroud)

任务2:

您需要通过以下方式安装virtualenv软件包:

python -m pip install --user virtualenv
Run Code Online (Sandbox Code Playgroud)

如果它已经存在那么:

pip install --upgrade virtualenv
Run Code Online (Sandbox Code Playgroud)

然后通过以下方式创建一个新的虚拟环境:

virtualenv your_virtual_environment --python=python3.12
Run Code Online (Sandbox Code Playgroud)

激活your_virtual_environment后您就可以安装软件包了。

其他注意事项

virtualenv喜欢缓存包以节省加载环境的时间,包括缓存旧版本的 pip

您的计算机中可能有旧的 pip 缓存版本。

您可能需要运行:

virtualenv --upgrade-embed-wheels
Run Code Online (Sandbox Code Playgroud)

virtualenv --reset-app-data <path_to_your_venv>
Run Code Online (Sandbox Code Playgroud)

更好的升级setuptools

python3.12 -m pip install --upgrade setuptools
Run Code Online (Sandbox Code Playgroud)

https://pythontest.com/posts/2023/2023-10-02-py312-impimporter/


引用自:https ://github.com/readthedocs/readthedocs.org/pull/10844

过时的 pip 版本仍然是主要问题,但过时的 pip 版本并非来自 python 安装,而是来自 virtualenv 创建。

创建环境时,virtualenv 会安装一些特定/过时的 [sic] 版本的 pip/setuptools/wheels。

虚拟环境


相关git:

https://github.com/pypa/pip/issues/11501

https://github.com/xlwings/xlwings/issues/2342

https://github.com/pypa/pip/issues/12179

https://github.com/readthedocs/readthedocs.org/issues/10832

https://github.com/pypa/setuptools/issues/3935

https://github.com/googleapis/gapic-generator-python/issues/1824

  • python3.12 -m pip install --upgrade setuptools 为我解决了这个问题,Ubuntu 22.04。 (2认同)

use*_*610 6

Python 删除了importlib. Python 3.12 发行说明中提到了这一点

importlib 中许多以前弃用的清理现已完成:

...

该错误消息意味着某些东西试图pkgutil.ImpImporter在软件包安装过程中使用已删除的内容。这可能是 pip、setuptools 或setup.py包本身中的安装脚本。查看堆栈跟踪,升级setuptools(如果使用虚拟环境,则在虚拟环境内)应该可以解决此问题。

另请注意 3.12 中的相关发行说明条目

gh-95299:不要在使用 venv 创建的虚拟环境中预安装 setuptools。这意味着默认情况下 distutils、setuptools、pkg_resources 和 easy_install 将不再可用;访问这些pip install setuptools在激活的虚拟环境中运行。