Mac M2环境下Spyder导入matplotlib失败的原因及解决方法
问题描述
- 设备:搭载ARM M2芯片的MacBook
- 系统:MacOS 15.3.1
- 环境:Anaconda + Python 3.11 + Spyder
- 问题:执行
import matplotlib.pyplot as plt时导入失败,更新matplotlib、Spyder、Python后问题依旧 - 关键现象:报错提示PIL的
_imaging模块无法加载依赖库libtiff.5.dylib,但已确认/Users/[NAME]/anaconda3/lib/python3.11/site-packages/PIL/_imaging.cpython-311-darwin.so文件存在
错误栈信息:
%runfile /Users/[NAME]/anaconda3/lib/python3.11/http/untitled2.py --wdir --------------------------------------------------------------------------- ImportError Traceback (most recent call last) File ~/anaconda3/lib/python3.11/site-packages/spyder_kernels/customize/utils.py:209, in exec_encapsulate_locals(code_ast, globals, locals, exec_fun, filename) 207 if filename is None: 208 filename = "<stdin>" --> 209 exec_fun(compile(code_ast, filename, "exec"), globals, None) 210 finally: 211 if use_locals_hack: 212 # Cleanup code File ~/anaconda3/lib/python3.11/http/untitled2.py:9 1 #!/usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 """ 4 Created on Sat Mar 15 15:12:11 2025 5 6 @author: [NAME] 7 """ ----> 9 import matplotlib.pyplot as plt File ~/anaconda3/lib/python3.11/site-packages/matplotlib/__init__.py:161 157 from packaging.version import parse as parse_version 159 # cbook must import matplotlib only within function 160 # definitions, so it is safe to import from it here. --> 161 from . import _api, _version, cbook, _docstring, rcsetup 162 from matplotlib._api import MatplotlibDeprecationWarning 163 from matplotlib.rcsetup import cycler # noqa: F401 File ~/anaconda3/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 ~/anaconda3/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 ~/anaconda3/lib/python3.11/site-packages/PIL/Image.py:84 75 MAX_IMAGE_PIXELS = int(1024 * 1024 * 1024 // 4 // 3) 78 try: 79 # If the _imaging C module is not present, Pillow will not load. 80 # Note that other modules should not refer to _imaging directly; 81 # import Image and use the Image.core variable instead. 82 # Also note that Image.core is not a publicly documented interface, 83 # and should be considered private and subject to change. ---> 84 from . import _imaging as core 86 if __version__ != getattr(core, "PILLOW_VERSION", None): 87 msg = ( 88 "The _imaging extension was built for another version of Pillow or PIL:\n" 89 f"Core version: {getattr(core, 'PILLOW_VERSION', None)}\n" 90 f"Pillow version: {__version__}" 91 ) ImportError: dlopen(/Users/[NAME]/anaconda3/lib/python3.11/site-packages/PIL/_imaging.cpython-311-darwin.so, 0x0002): Library not loaded: @rpath/libtiff.5.dylib Referenced from: <B3C0C665-A4DE-3282-A587-6B0B5F8FD71C> /Users/[NAME]/anaconda3/lib/python3.11/site-packages/PIL/_imaging.cpython-311-darwin.so Reason: tried: '/Users/[NAME]/anaconda3/lib/python3.11/site-packages/PIL/../../../libtiff.5.dylib' (no such file), '/Users/[NAME]/anaconda3/lib/python3.11/site-packages/PIL/../../../libtiff.5.dylib' (no such file), '/Users/[NAME]/anaconda3/bin/../lib/libtiff.5.dylib' (no such file), '/Users/[NAME]/anaconda3/bin/../lib/libtiff.5.dylib' (no such file), '/usr/local/lib/libtiff.5.dylib' (no such file), '/usr/lib/libtiff.5.dylib' (no such file, not in dyld cache)
错误原因
- 核心问题:Pillow的C扩展模块
_imaging.cpython-311-darwin.so依赖的libtiff.5.dylib库缺失,或库的路径引用无效 - 可能诱因:Anaconda环境更新过程中依赖库损坏/丢失,或ARM架构下编译的库存在路径适配问题,导致
@rpath指向的位置找不到目标库
解决方法
按以下步骤依次尝试,优先使用Anaconda命令维护环境:
1. 重新安装libtiff依赖库
打开Anaconda终端,执行:
conda install -c conda-forge libtiff --force-reinstall
如果conda安装失败,可尝试用pip:
pip install --force-reinstall libtiff
2. 强制重新安装Pillow库
重新编译Pillow的C扩展,确保依赖路径正确:
conda install -c conda-forge pillow --force-reinstall
或pip方式:
pip install --force-reinstall pillow
3. 修复Anaconda环境完整性
清理缓存并更新所有包:
conda clean --all conda update --all
若仍无效,创建全新的Anaconda环境测试:
conda create -n py311_matplotlib python=3.11 matplotlib spyder conda activate py311_matplotlib
在新环境中启动Spyder,测试import matplotlib.pyplot as plt
4. 手动创建库软链接(仅上述方法无效时使用)
先查找Anaconda环境中已有的libtiff库:
find ~/anaconda3 -name "libtiff*.dylib"
若找到如libtiff.6.dylib,则创建指向libtiff.5.dylib的软链接(替换路径为实际找到的位置):
ln -s /Users/[NAME]/anaconda3/lib/libtiff.6.dylib /Users/[NAME]/anaconda3/lib/libtiff.5.dylib
内容的提问来源于stack exchange,提问作者LVT151012
相关产品推荐
相关产品推荐

