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

Windows11环境下Jupyter Notebook调用matplotlib致内核崩溃求助

解决Windows11下Anaconda虚拟环境中Jupyter内核因OpenMP冲突崩溃的问题

问题描述

我是Python和Jupyter Notebook新手,在Windows11电脑上安装了Anaconda,于虚拟环境中运行Jupyter Notebook。运行以下代码时内核崩溃:

import torch
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import torch.nn as nn

X = torch.linspace(1,50,50).reshape(-1,1)
torch.manual_seed(71)
e = torch.randint(-8,9,(50,1),dtype=torch.float)
y = 2*X + 1 + e

plt.scatter(X.numpy(),y.numpy())

报错信息:The kernel appears to have died. It will restart automatically.

已定位问题出在plt.scatter调用,单独执行该单元格会导致崩溃。测试发现打印Hello World、使用numpy和torch均正常。

内核崩溃时命令窗口提示OpenMP库重复初始化:

[I 16:30:08.979 NotebookApp] Kernel started: 503c71ae-ced0-4147-8f23-7cdad416d503, name: python3
OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.
OMP: Hint This means that multiple copies of the OpenMP runtime have been linked into the program. That is dangerous, since it can degrade performance or cause incorrect results. The best thing to do is to ensure that only a single OpenMP runtime is linked into the process, e.g. by avoiding static linking of the OpenMP runtime in any library. As an unsafe, unsupported, undocumented workaround you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the program to continue to execute, but that may cause crashes or silently produce incorrect results.
[I 16:30:23.983 NotebookApp] KernelRestarter: restarting kernel (1/5), new random ports
WARNING:root:kernel 503c71ae-ced0-4147-8f23-7cdad416d503 restarted

已尝试的解决方案:

  • 管理员身份运行Anaconda Prompt执行conda install --yes freetype=2.10.4,提示所有包已安装
  • 执行conda update mkl,问题未解决

解决方案

1. 设置环境变量KMP_DUPLICATE_LIB_OK=TRUE(临时 workaround)

这是错误提示中提到的临时解决办法,步骤如下:

  • 打开Windows系统设置,搜索「环境变量」
  • 在「系统变量」区域点击「新建」
  • 变量名填KMP_DUPLICATE_LIB_OK,变量值填TRUE
  • 保存后关闭所有Anaconda/Jupyter窗口,重新启动虚拟环境和Jupyter Notebook测试

2. 统一虚拟环境中的OpenMP依赖

冲突通常来自不同包依赖的OpenMP版本不一致,可尝试重新安装相关包:

  1. 激活你的虚拟环境:
conda activate your_env_name
  1. 卸载并重新安装matplotlib和mkl:
conda uninstall matplotlib mkl -y
conda install matplotlib mkl -y
  1. 检查torch是否和当前环境的mkl兼容,若仍有问题,尝试重新安装torch:
conda uninstall pytorch torchvision torchaudio -y
conda install pytorch torchvision torchaudio cpuonly -c pytorch  # GPU版本请替换为对应安装命令

3. 排查系统中重复的libiomp5md.dll文件

  • 搜索系统盘(通常为C盘)中的libiomp5md.dll文件
  • 除了Anaconda虚拟环境目录下的文件,若其他路径(如全局Python环境、第三方软件目录)也存在该文件,可暂时重命名非Anaconda路径下的文件,测试是否解决冲突

内容的提问来源于stack exchange,提问作者oticetam

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 23:46:01