解决Python3.12版本使用pip安装包命令pkgutil报错
使用命令python -m pip install --upgrade pip
时出现如下错误
ERROR: Exception:
Traceback (most recent call last):
File "xxxx\venv\Lib\site-packages\pip\_internal\cli\base_command.py", line 160, in exc_logging_wrapper
status = run_func(*args)
^^^^^^^^^^^^^^^
File "xxxx\venv\Lib\site-packages\pip\_internal\cli\req_command.py", line 247, in wrapper
return func(self, options, args)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "xxxx\venv\Lib\site-packages\pip\_internal\commands\install.py", line 326, in run
session = self.get_default_session(options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "xxxx\venv\Lib\site-packages\pip\_internal\cli\req_command.py", line 98, in get_default_session
self._session = self.enter_context(self._build_session(options))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "xxxx\venv\Lib\site-packages\pip\_internal\cli\req_command.py", line 125, in _build_session
session = PipSession(
^^^^^^^^^^^
File "xxxx\venv\Lib\site-packages\pip\_internal\network\session.py", line 343, in __init__
self.headers["User-Agent"] = user_agent()
^^^^^^^^^^^^
File "xxxx\venv\Lib\site-packages\pip\_internal\network\session.py", line 175, in user_agent
setuptools_dist = get_default_environment().get_distribution("setuptools")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "xxxx\venv\Lib\site-packages\pip\_internal\metadata\importlib\_envs.py", line 188, in get_distribution
return next(matches, None)
^^^^^^^^^^^^^^^^^^^
File "xxxx\venv\Lib\site-packages\pip\_internal\metadata\importlib\_envs.py", line 183, in <genexpr>
matches = (
^
File "xxxx\venv\Lib\site-packages\pip\_internal\metadata\base.py", line 612, in iter_all_distributions
for dist in self._iter_distributions():
File "xxxx\venv\Lib\site-packages\pip\_internal\metadata\importlib\_envs.py", line 176, in _iter_distributions
for dist in finder.find_eggs(location):
File "xxxx\venv\Lib\site-packages\pip\_internal\metadata\importlib\_envs.py", line 144, in find_eggs
yield from self._find_eggs_in_dir(location)
File "xxxx\venv\Lib\site-packages\pip\_internal\metadata\importlib\_envs.py", line 111, in _find_eggs_in_dir
from pip._vendor.pkg_resources import find_distributions
File "xxxx\venv\Lib\site-packages\pip\_vendor\pkg_resources\__init__.py", line 2164, in <module>
register_finder(pkgutil.ImpImporter, find_on_path)
^^^^^^^^^^^^^^^^^^^
AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'?
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "xxxx\venv\Lib\site-packages\pip\__main__.py", line 31, in <module>
sys.exit(_main())
^^^^^^^
File "xxxx\venv\Lib\site-packages\pip\_internal\cli\main.py", line 70, in main
return command.main(cmd_args)
^^^^^^^^^^^^^^^^^^^^^^
File "xxxx\venv\Lib\site-packages\pip\_internal\cli\base_command.py", line 101, in main
return self._main(args)
^^^^^^^^^^^^^^^^
File "xxxx\venv\Lib\site-packages\pip\_internal\cli\base_command.py", line 216, in _main
self.handle_pip_version_check(options)
File "xxxx\venv\Lib\site-packages\pip\_internal\cli\req_command.py", line 179, in handle_pip_version_check
session = self._build_session(
^^^^^^^^^^^^^^^^^^^^
File "xxxx\venv\Lib\site-packages\pip\_internal\cli\req_command.py", line 125, in _build_session
session = PipSession(
^^^^^^^^^^^
File "xxxx\venv\Lib\site-packages\pip\_internal\network\session.py", line 343, in __init__
self.headers["User-Agent"] = user_agent()
^^^^^^^^^^^^
File "xxxx\venv\Lib\site-packages\pip\_internal\network\session.py", line 175, in user_agent
setuptools_dist = get_default_environment().get_distribution("setuptools")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "xxxx\venv\Lib\site-packages\pip\_internal\metadata\importlib\_envs.py", line 188, in get_distribution
return next(matches, None)
^^^^^^^^^^^^^^^^^^^
File "xxxx\venv\Lib\site-packages\pip\_internal\metadata\importlib\_envs.py", line 183, in <genexpr>
matches = (
^
File "xxxx\venv\Lib\site-packages\pip\_internal\metadata\base.py", line 612, in iter_all_distributions
for dist in self._iter_distributions():
File "xxxx\venv\Lib\site-packages\pip\_internal\metadata\importlib\_envs.py", line 176, in _iter_distributions
for dist in finder.find_eggs(location):
File "xxxx\venv\Lib\site-packages\pip\_internal\metadata\importlib\_envs.py", line 144, in find_eggs
yield from self._find_eggs_in_dir(location)
File "xxxx\venv\Lib\site-packages\pip\_internal\metadata\importlib\_envs.py", line 111, in _find_eggs_in_dir
from pip._vendor.pkg_resources import find_distributions
File "xxxx\venv\Lib\site-packages\pip\_vendor\pkg_resources\__init__.py", line 2164, in <module>
register_finder(pkgutil.ImpImporter, find_on_path)
^^^^^^^^^^^^^^^^^^^
AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'?
原因是python 3.12中自带的 pip 版本较低(一般为22.x.x),旧版本pip使用了在 python 3.12 中删除的废弃 API pkgutil.ImpImporter
,产生了module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'?
这项错误
解决方案: 使用下面的命令更新pip后, 不再报错
python -m ensurepip --upgrade
注意: 不能使用
python -m pip install --upgrade pip
这种常规更新方法, 因为这个命令还是会调用pip本身来更新, 依然会报同样的错误