解决Python3.12版本使用pip安装包命令pkgutil报错

文章讲述了在Python3.12环境中使用pip安装包时遇到的关于pkgutil.ImpImporter的错误,原因是pip版本过低,解决办法是使用`python-mensurepip--upgrade`而非`python-mpipinstall--upgradepip`。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

解决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本身来更新, 依然会报同样的错误

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值