matplotlib安装成功后引入报错问题解决

matplotlib是Python主要的科学绘图库,将数据及各种分析生可视化,如折线图、直方图、散点图等。安装时遇到点小问题,运行安装命令:pip3 install matplotlib ,提示安装成功了。但是引入该包时却提示错误:

ImportError         Traceback (most recent call last)
Cell In[11], line 1
----> 1 import matplotlib
      2 print(matplotlib.__version__)

File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/matplotlib/__init__.py:159
    155 from packaging.version import parse as parse_version
    157 # cbook must import matplotlib only within function
    158 # definitions, so it is safe to import from it here.
--> 159 from . import _api, _version, cbook, _docstring, rcsetup
    160 from matplotlib.cbook import sanitize_sequence
    161 from matplotlib._api import MatplotlibDeprecationWarning

File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/matplotlib/rcsetup.py:28
     26 from matplotlib.backends import BackendFilter, backend_registry
     27 from matplotlib.cbook import ls_mapper
---> 28 from matplotlib.colors import Colormap, is_color_like
     29 from matplotlib._fontconfig_pattern import parse_fontconfig_pattern
     30 from matplotlib._enums import JoinStyle, CapStyle

File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/matplotlib/colors.py:52
     49 from numbers import Real
     50 import re
---> 52 from PIL import Image
     53 from PIL.PngImagePlugin import PngInfo
     55 import matplotlib as mpl

File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/PIL/Image.py:100
     91 MAX_IMAGE_PIXELS = int(1024 * 1024 * 1024 // 4 // 3)
     94 try:
     95     # If the _imaging C module is not present, Pillow will not load.
     96     # Note that other modules should not refer to _imaging directly;
     97     # import Image and use the Image.core variable instead.
     98     # Also note that Image.core is not a publicly documented interface,
     99     # and should be considered private and subject to change.
--> 100     from . import _imaging as core
    102     if __version__ != getattr(core, "PILLOW_VERSION", None):
    103         raise ImportError(
    104             "The _imaging extension was built for another version of Pillow or PIL:\n"
    105             f"Core version: {getattr(core, 'PILLOW_VERSION', None)}\n"
    106             f"Pillow version: {__version__}"
    107         )

ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/PIL/_imaging.cpython-311-darwin.so, 0x0002): tried: '/usr/local/mysql/lib/_imaging.cpython-311-darwin.so' (no such file), '/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/PIL/_imaging.cpython-311-darwin.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64')), '/System/Volumes/Preboot/Cryptexes/OS/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/PIL/_imaging.cpython-311-darwin.so' (no such file), '/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/PIL/_imaging.cpython-311-darwin.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64'))

问题原因:本机安装的Pillow库与Python解释器或操作系统架构不兼容,版本太低了

重新安装Pillow,执行:pip3 install --upgrade --force-reinstall pillow来尝试重新安装Pillow。或者许先卸载:pip3 uninstall Pillow,再安装:pip3 install Pillow。

又出现新的报错:

ImportError                               Traceback (most recent call last)
File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/PIL/Image.py:96
     91         msg = (
     92             "The _imaging extension was built for another version of Pillow or PIL:\n"
     93             f"Core version: {getattr(core, 'PILLOW_VERSION', None)}\n"
     94             f"Pillow version: {__version__}"
     95         )
---> 96         raise ImportError(msg)
     98 except ImportError as v:

ImportError: The _imaging extension was built for another version of Pillow or PIL:
Core version: 10.3.0
Pillow version: 9.3.0

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
Cell In[33], line 1
----> 1 import matplotlib as pl
      2 print("matplotlib version:{}".format(pl.__version__))

File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/matplotlib/__init__.py:159
    155 from packaging.version import parse as parse_version
    157 # cbook must import matplotlib only within function
    158 # definitions, so it is safe to import from it here.
--> 159 from . import _api, _version, cbook, _docstring, rcsetup
    160 from matplotlib.cbook import sanitize_sequence
    161 from matplotlib._api import MatplotlibDeprecationWarning

File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/matplotlib/rcsetup.py:28
     26 from matplotlib.backends import BackendFilter, backend_registry
     27 from matplotlib.cbook import ls_mapper
---> 28 from matplotlib.colors import Colormap, is_color_like
     29 from matplotlib._fontconfig_pattern import parse_fontconfig_pattern
     30 from matplotlib._enums import JoinStyle, CapStyle

File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/matplotlib/colors.py:52
     49 from numbers import Real
     50 import re
---> 52 from PIL import Image
     53 from PIL.PngImagePlugin import PngInfo
     55 import matplotlib as mpl

File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/PIL/Image.py:99
     96         raise ImportError(msg)
     98 except ImportError as v:
---> 99     core = DeferredError.new(ImportError("The _imaging C module is not installed."))
    100     # Explanations for ways that we know we might have an import error
    101     if str(v).startswith("Module use of python"):
    102         # The _imaging C module is present, but not compiled for
    103         # the right version (windows only).  Print a warning, if
    104         # possible.

AttributeError: type object 'DeferredError' has no attribute 'new'

问题原因:matplotlib安装时依赖的Pillow是9.3.0版本,现在Pillow包更新成最新的10.3.0版本,导致matplotlib与Pillow的版本不一致。

解决方案:重新安装matplotlib,先卸载:pip3 uninstall matplotlib,再安装:pip3 install matplotlib

问题这才解决。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值