是什么导致我尝试导入matplotlib失败,我该如何修复它? 我很抱歉,如果这是一个菜鸟问题,但是: 从本周开始,我一直无法通过Spyder进口Matplotlib。我以前没有这个问题。 我正在使用MacBook运行MacOS 15.3.1 ...

问题描述 投票:0回答:1
我收到的错误。我已经用“ [name]”代替了我的用户名的所有实例。

注意,/user/ [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)```

如果您使用的是Anaconda,请确保它是本机ARM版本(M1/M2兼容)。默认的Anaconda发行版是为Intel(X86_64)构建的,可能会在M1/M2 MACS上引起问题

解开当前的anaconda(如果不是手臂的话):

rm -rf ~/anaconda3

安装miniforge

# Download and install Miniforge curl -L -O https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-MacOSX-arm64.sh bash Miniforge3-MacOSX-arm64.sh
创建具有臂章兼容套件的新环境:
python-3.x matplotlib importerror anaconda3
1个回答
0
投票
conda create -n myenv python=3.11 conda activate myenv

使用Conda-Forge安装必要的包装(Matplotlib,枕头和Libtiff),该包装提供与手臂兼容的版本:

conda install -c conda-forge matplotlib pillow libtiff
如果您以前使用Rosetta 2(Intel X86_64仿真)安装了Anaconda或Python,则可能会引起冲突。确保您正在运行python的本机臂版:

which python3
输出应指向您的臂线miniforge安装中的路径(例如,

~/miniforge3/bin/python3

)。
如果指向英特尔版本(例如,
/usr/bin/python3

),则应停用Rosetta 2并确保使用Arm-native版本。

检查是否正确安装了libtiff:
conda list libtiff

如果未列出,请明确安装:
conda install -c conda-forge libtiff

Optional:如果Libtiff的Conda安装不起作用,则可以尝试通过Homebrew进行安装:

brew install libtiff

,然后确保Python可以通过设置适当的环境变量来找到自制的库:

export DYLD_LIBRARY_PATH=/opt/homebrew/lib:$DYLD_LIBRARY_PATH

完成上述步骤后,再次测试您的脚本:

python3 /Users/[NAME]/anaconda3/lib/python3.11/http/untitled2.py

© www.soinside.com 2019 - 2025. All rights reserved.