您的位置:首页 > 新闻 > 资讯 > matplotlib安装成功后引入报错问题解决

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

2025/3/12 12:24:23 来源:https://blog.csdn.net/lcz_2/article/details/139840565  浏览:    关键词:matplotlib安装成功后引入报错问题解决

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

ImportError         Traceback (most recent call last)
Cell In[11], line 1
----> 1 import matplotlib2 print(matplotlib.__version__)File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/matplotlib/__init__.py:159155 from packaging.version import parse as parse_version157 # cbook must import matplotlib only within function158 # definitions, so it is safe to import from it here.
--> 159 from . import _api, _version, cbook, _docstring, rcsetup160 from matplotlib.cbook import sanitize_sequence161 from matplotlib._api import MatplotlibDeprecationWarningFile /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/matplotlib/rcsetup.py:2826 from matplotlib.backends import BackendFilter, backend_registry27 from matplotlib.cbook import ls_mapper
---> 28 from matplotlib.colors import Colormap, is_color_like29 from matplotlib._fontconfig_pattern import parse_fontconfig_pattern30 from matplotlib._enums import JoinStyle, CapStyleFile /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/matplotlib/colors.py:5249 from numbers import Real50 import re
---> 52 from PIL import Image53 from PIL.PngImagePlugin import PngInfo55 import matplotlib as mplFile /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/PIL/Image.py:10091 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 core102     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:9691         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.0During handling of the above exception, another exception occurred:AttributeError                            Traceback (most recent call last)
Cell In[33], line 1
----> 1 import matplotlib as pl2 print("matplotlib version:{}".format(pl.__version__))File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/matplotlib/__init__.py:159155 from packaging.version import parse as parse_version157 # cbook must import matplotlib only within function158 # definitions, so it is safe to import from it here.
--> 159 from . import _api, _version, cbook, _docstring, rcsetup160 from matplotlib.cbook import sanitize_sequence161 from matplotlib._api import MatplotlibDeprecationWarningFile /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/matplotlib/rcsetup.py:2826 from matplotlib.backends import BackendFilter, backend_registry27 from matplotlib.cbook import ls_mapper
---> 28 from matplotlib.colors import Colormap, is_color_like29 from matplotlib._fontconfig_pattern import parse_fontconfig_pattern30 from matplotlib._enums import JoinStyle, CapStyleFile /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/matplotlib/colors.py:5249 from numbers import Real50 import re
---> 52 from PIL import Image53 from PIL.PngImagePlugin import PngInfo55 import matplotlib as mplFile /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/PIL/Image.py:9996         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 error101     if str(v).startswith("Module use of python"):102         # The _imaging C module is present, but not compiled for103         # the right version (windows only).  Print a warning, if104         # 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

问题这才解决。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com