前言
目前现有的很多Python代码都是基于Python3.8、或者Python3.9的甚至是更早的版本。
当我们用最新的Python3.12来跑这些程序的时候,就会出现很多兼容性的问题。
本文就对“'ImpImporter'”和“'zipimporter'”的替换问题给出了一个解决方案。
1、错误描述
AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'?
这个错误如何解决:
具体错误的描述
ERROR: Exception:
Traceback (most recent call last):
File "D:\PycharmProjects\py312_t1\venv\Lib\site-packages\pip\_internal\cli\base_command.py", line 160, in exc_logging_wrapper
status = run_func(*args)
^^^^^^^^^^^^^^^
File "D:\PycharmProjects\py312_t1\venv\Lib\site-packages\pip\_internal\commands\list.py", line 169, in run
packages: "_ProcessedDists" = [
^
File "D:\PycharmProjects\py312_t1\venv\Lib\site-packages\pip\_internal\metadata\base.py", line 664, in <genexpr>
return (d for d in it if d.canonical_name not in skip)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\PycharmProjects\py312_t1\venv\Lib\site-packages\pip\_internal\metadata\base.py", line 612, in iter_all_distributions
for dist in self._iter_distributions():
File "D:\PycharmProjects\py312_t1\venv\Lib\site-packages\pip\_internal\metadata\importlib\_envs.py", line 176, in _iter_distributions
for dist in finder.find_eggs(location):
File "D:\PycharmProjects\py312_t1\venv\Lib\site-packages\pip\_internal\metadata\importlib\_envs.py", line 146, in find_eggs
yield from self._find_eggs_in_zip(location)
File "D:\PycharmProjects\py312_t1\venv\Lib\site-packages\pip\_internal\metadata\importlib\_envs.py", line 123, in _find_eggs_in_zip
from pip._vendor.pkg_resources import find_eggs_in_zip
File "D:\PycharmProjects\py312_t1\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'?
2、错误的产生
当Pycharm打开新建Python3.12.4的工程的时候,
比如,我想导入一个opencv包
pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple
发现产生一堆的错误,如下:
即使,使用pip list 也是同样的错误。
想要升级pip:pip install --upgrade pip
也是同样的错误。
咋办?
3、解决的办法
找到文件:
将这行改掉:
改为:
register_finder(zipimport.zipimporter, find_on_path)
如下图:
这一处也改掉,如下图:
是不是,很大胆直接和暴力。
改完以后,发现pip install 就能用了,如下图: