AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'?

回答 2 浏览 8715 2023-10-26

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

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

py -m venv pyspedas
.\pyspedas\Scripts\activate
pip install pyspedas

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

Collecting pyspedas
  Using cached pyspedas-1.4.47-py3-none-any.whl.metadata (14 kB)
Collecting numpy>=1.19.5 (from pyspedas)
  Using cached numpy-1.26.1-cp312-cp312-win_amd64.whl.metadata (61 kB)
Collecting requests (from pyspedas)
  Using cached requests-2.31.0-py3-none-any.whl.metadata (4.6 kB)
Collecting geopack>=1.0.10 (from pyspedas)
  Using cached geopack-1.0.10-py3-none-any.whl (114 kB)
Collecting cdflib<1.0.0 (from pyspedas)
  Using cached cdflib-0.4.9-py3-none-any.whl (72 kB)
Collecting cdasws>=1.7.24 (from pyspedas)
  Using cached cdasws-1.7.43.tar.gz (21 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Collecting netCDF4>=1.6.2 (from pyspedas)
  Using cached netCDF4-1.6.5-cp312-cp312-win_amd64.whl.metadata (1.8 kB)
Collecting pywavelets (from pyspedas)
  Using cached PyWavelets-1.4.1.tar.gz (4.6 MB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
  error: subprocess-exited-with-error

  × Getting requirements to build wheel did not run successfully.
  │ exit code: 1
  ╰─> [33 lines of output]
      Traceback (most recent call last):
        File "C:\Users\UserName\pyspedas\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 353, in <module>
          main()
        File "C:\Users\UserName\pyspedas\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 335, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        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
          backend = _build_backend()
                    ^^^^^^^^^^^^^^^^
        File "C:\Users\UserName\pyspedas\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 77, in _build_backend
          obj = import_module(mod_path)
                ^^^^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\UserName\AppData\Local\Programs\Python\Python312\Lib\importlib\__init__.py", line 90, in import_module
          return _bootstrap._gcd_import(name[level:], package, level)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "<frozen importlib._bootstrap>", line 1381, in _gcd_import
        File "<frozen importlib._bootstrap>", line 1354, in _find_and_load
        File "<frozen importlib._bootstrap>", line 1304, in _find_and_load_unlocked
        File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
        File "<frozen importlib._bootstrap>", line 1381, in _gcd_import
        File "<frozen importlib._bootstrap>", line 1354, in _find_and_load
        File "<frozen importlib._bootstrap>", line 1325, in _find_and_load_unlocked
        File "<frozen importlib._bootstrap>", line 929, in _load_unlocked
        File "<frozen importlib._bootstrap_external>", line 994, in exec_module
        File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
        File "C:\Users\UserName\AppData\Local\Temp\pip-build-env-_lgbq70y\overlay\Lib\site-packages\setuptools\__init__.py", line 16, in <module>
          import setuptools.version
        File "C:\Users\UserName\AppData\Local\Temp\pip-build-env-_lgbq70y\overlay\Lib\site-packages\setuptools\version.py", line 1, in <module>
          import pkg_resources
        File "C:\Users\UserName\AppData\Local\Temp\pip-build-env-_lgbq70y\overlay\Lib\site-packages\pkg_resources\__init__.py", line 2191, in <module>
          register_finder(pkgutil.ImpImporter, find_on_path)
                          ^^^^^^^^^^^^^^^^^^^
      AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'?
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.

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

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

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

Collecting pywavelets (from pyspedas)
  Using cached PyWavelets-1.4.1.tar.gz (4.6 MB)
  Installing build dependencies ... done

我在 Windows 11 中使用 IDLE。

sreeraj t 提问于2023-10-26
2 个回答
#1楼 已采纳
得票数 13

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


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


AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: '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

任务1:

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

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

Linux/MacOS 终端上:

python -m ensurepip --upgrade

Windows上:

py -m ensurepip --upgrade

任务2:

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

python -m pip install --user virtualenv

如果它已经存在那么:

pip install --upgrade virtualenv

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

virtualenv your_virtual_environment --python=python3.12

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

其他注意事项

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

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

您可能需要运行:

virtualenv --upgrade-embed-wheels

virtualenv --reset-app-data

最好也升级setuptools

python3.12 -m pip install --upgrade setuptools

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


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

Having an outdated version of pip is still the main problem, but the outdated version of pip didn't come from the python installation, but from the virtualenv creation.

When creating the environment, virtualenv installs some specfic/outdated versions of pip/setuptools/wheels.

virtualenv


相关文章:

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

Goku - stands with Palestine 提问于2023-10-26
TylerH 修改于2023-11-13
感谢您的回复。我执行了命令C:\Users\UserName>pip -VC:\Users\UserName>py -m ensurepip --upgrade,其响应是pip 23.3.1 from C:\Users\UserName\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip (python 3.12)Looking in links: c:\Users\UserName\AppData\Local\Temp\tmp23bccewt Requirement already satisfied: pip in c:\users\UserName\appdata\local\programs\python\python312\lib\site-packages (23.3.1).但问题仍然存在。sreeraj t 2023-10-26
感谢您的快速回复。但还是没有。我执行的命令是py -m ensurepip --upgradepy -m pippy -m pip install pyspedas(我没有使用py -m venv pyspedas)。此外,空间不应该在那里-- upgrade?它给我造成了一个错误。sreeraj t 2023-10-26
我衷心感谢您的所有帮助。我按以下顺序执行命令:pip install --upgrade virtualenv --> virtualenv venv123 --python=python3.12 --> venv123\Scripts\activate.bat --> pip install pyspedas。但依然坚持。您认为安装旧版本可以解决问题吗?sreeraj t 2023-10-26
#2楼
得票数 1

Python 删除了 importlib 中一些以前不推荐使用的符号。 Python 3.12 发行说明中提到了这一点

Many previously deprecated cleanups in importlib have now been completed:

...

  • importlib.abc.Finder, pkgutil.ImpImporter, and pkgutil.ImpLoader have been removed. (Contributed by Barry Warsaw in gh-98040.)

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

另请注意此相关发行说明条目

gh-95299: Do not pre-install setuptools in virtual environments created with venv. This means that distutils, setuptools, pkg_resources, and easy_install will no longer available by default; to access these run pip install setuptools in the activated virtual environment.

user7610 提问于2023-11-11