You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jupyter Notebook无法导入matplotlib,NumPy版本冲突问题求助

问题:虚拟环境中Jupyter Notebook无法识别降级后的NumPy版本,引发Matplotlib导入错误

环境背景

  • Python 3.9虚拟环境(运行于CentOS 7系统)
  • 激活环境后,通过python3终端可正常导入matplotlib,初始NumPy版本为2.0.2
  • 启动Jupyter Notebook时触发NumPy版本兼容报错,具体错误信息如下:

A module that was compiled using NumPy 1.x cannot be run in
NumPy 2.0.2 as it may crash. To support both 1.x and 2.x
versions of NumPy, modules must be compiled with NumPy 2.0.
Some module may need to rebuild instead e.g. with 'pybind11>=2.12'.

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.

Traceback (most recent call last): File "/usr/people/shared/tools/centos/7/python/3.9.9/lib/python3.9/runpy.py", line 197, in _run_module_as_main
..........

/usr/people/shared/tools/centos/7/python/3.9.9/lib/python3.9/site-packages/matplotlib/transforms.py in
44
45 from matplotlib import _api
---> 46 from matplotlib._path import (
47 affine_transform, count_bboxes_overlapping_bbox, update_path_extents)
48 from .path import Path

ImportError: numpy.core.multiarray failed to import

已尝试的无效操作

  • 执行pip3 install --force-reinstall numpy=="1.26.0"将NumPy降级至1.26.0
  • 重启Jupyter内核
  • 问题未解决:Jupyter始终加载NumPy 2.0.2,不受虚拟环境中安装版本影响

解决方案

1. 绑定虚拟环境到Jupyter内核

Jupyter可能未使用当前激活的虚拟环境,需先在虚拟环境中安装并注册内核:

source ./python3.9_venv/bin/activate
pip3 install ipykernel
python3 -m ipykernel install --user --name=python3.9_venv --display-name="Python 3.9 (venv)"

完成后在Jupyter Notebook中切换至Python 3.9 (venv)内核。

2. 彻底清理并重装依赖

CentOS 7的旧系统可能存在缓存残留,执行以下命令彻底重置:

# 激活虚拟环境
source ./python3.9_venv/bin/activate
# 卸载所有NumPy版本
pip3 uninstall -y numpy
# 清理pip缓存
pip3 cache purge
# 重新安装指定NumPy版本
pip3 install numpy==1.26.0
# 更新matplotlib以适配NumPy 1.x
pip3 install --upgrade matplotlib

3. 屏蔽系统级Python干扰

系统共享Python路径/usr/people/shared/tools/centos/7/python/3.9.9/可能优先加载库,需修改虚拟环境激活脚本强制优先使用本地库:
在./python3.9_venv/bin/activate末尾添加:

export PYTHONPATH="./python3.9_venv/lib/python3.9/site-packages:$PYTHONPATH"

保存后重新激活环境,再启动Jupyter。


内容的提问来源于stack exchange,提问作者K.defaoite

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 03:15:12