Matplotlib导入失败求助:已调整NumPy版本仍报错
解决Matplotlib导入错误问题
问题现象
导入Matplotlib时触发错误:
If you are a user of the module, the easiest solution will be to
downgrade to 'numpy<2' or try to upgrade the affected module.
We expect that some modules will need time to support NumPy 2.
当前已安装包版本:
- matplotlib 3.9.0
- matplotlib-inline 0.1.7
- numpy 1.26.4
已尝试卸载重装、干净虚拟环境安装,但问题仍存在。
可行解决步骤
确认当前运行环境的numpy版本
有时pip list显示的是全局环境版本,而实际运行代码的是另一个Python环境。在报错的Python环境中执行:import numpy print(numpy.__version__)确认是否真的为1.26.4,而非意外安装的numpy 2.x版本。
强制重新安装兼容版本组合
直接指定matplotlib和numpy的兼容版本进行安装,覆盖现有包:pip install --force-reinstall numpy==1.26.4 matplotlib==3.9.0 matplotlib-inline==0.1.7--force-reinstall会彻底重新下载安装包,避免缓存导致的版本异常。清理pip缓存
如果缓存中存在损坏的安装包,可能导致安装异常,执行清理:pip cache purge之后再重新安装上述指定版本的包。
检查其他依赖包冲突
查看matplotlib的依赖树,确认是否有其他包强制依赖numpy 2.x:pip show matplotlib查看
Requires字段,matplotlib 3.9.0的numpy依赖为>=1.23且<2,1.26.4符合要求。如果有其他包要求numpy>=2,需卸载该包或寻找其兼容版本。
内容的提问来源于stack exchange,提问作者wanderer
相关产品推荐
相关产品推荐

