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

Windows环境下运行Python代码时内核(Kernel)崩溃自动重启问题求助

解决Windows下Matplotlib导致Jupyter内核崩溃的问题

Hey there, sorry to hear you're stuck with this kernel crash when running matplotlib on Windows. Let's go through some practical fixes that usually get this sorted out.

常见排查与解决步骤

  • 调整Matplotlib后端设置
    Windows环境下,matplotlib默认的后端偶尔会和Jupyter环境不兼容,这是内核崩溃的常见原因。你可以手动指定一个更稳定的后端试试:

    import matplotlib
    # 先尝试TkAgg,这是Windows上兼容性较好的后端
    matplotlib.use('TkAgg')
    import matplotlib.pyplot as plt
    plt.plot()
    

    如果TkAgg还是不行,再试试'Qt5Agg'(需要先安装pyqt5:pip install pyqt5)或者'Agg'(非交互式后端,适合生成静态图片,不会弹出绘图窗口)。

  • 更新/重装相关依赖库
    版本不匹配或者库文件损坏也会引发这类崩溃。先尝试更新所有相关库:

    pip install --upgrade pandas numpy matplotlib
    

    如果更新后问题依旧,彻底卸载后重新安装:

    pip uninstall -y pandas numpy matplotlib
    pip install pandas numpy matplotlib
    
  • 确认Python环境与Jupyter内核一致性
    有时候虚拟环境和系统环境混用时,Jupyter内核可能指向了未安装对应库的Python解释器。你可以在Jupyter里运行这段代码检查路径:

    import sys
    print(sys.executable)
    

    输出的路径应该和你安装pandas、matplotlib的那个Python解释器路径一致。如果不一致,需要重新配置Jupyter内核,指向正确的环境。

  • 禁用硬件加速(针对显卡驱动问题)
    部分显卡驱动和matplotlib的硬件加速不兼容,会导致内核崩溃。可以通过代码临时禁用:

    import matplotlib
    matplotlib.rcParams['backend'] = 'Agg'
    # 或者结合前面的后端设置,同时关闭硬件加速相关配置
    matplotlib.use('TkAgg')
    matplotlib.rcParams['figure.dpi'] = 100
    import matplotlib.pyplot as plt
    plt.plot()
    
  • 检查系统依赖
    Matplotlib在Windows上需要依赖Visual C++ Redistributable Packages。如果你的系统没有安装对应版本(和Python位数一致,32/64位),可能会导致底层崩溃。可以安装微软官方的VC++ redist包后再尝试运行代码。

Give these steps a try one by one—usually adjusting the backend or updating libraries resolves this issue quickly.

内容的提问来源于stack exchange,提问作者ngoulou likibi chris

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 21:39:13