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

PyCharm调试模式下sklearn的roc_auc_score报错 正常运行无异常

问题现象
  • 使用roc_auc_score评估真实标签、预测结果两组数组的AUC指标时,代码在PyCharm普通运行模式下可正常执行,切换至调试模式时抛出异常
  • 运行到roc_auc_score代码行前手动暂停程序,在调试控制台仅传入两个小型测试数组调用该函数,仍然会复现相同问题
  • 相同的函数调用逻辑在普通Python控制台中执行完全正常
报错栈信息
Traceback (most recent call last):
  File "C:\Users\User\anaconda3\envs\project\lib\site-packages\numpy\core\getlimits.py", line 649, in __init__
    self.dtype = numeric.dtype(int_type)
TypeError: 'NoneType' object is not callable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm 2022.1.2\plugins\python\helpers\pydev\pydevd.py", line 1491, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm 2022.1.2\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/User/PycharmProjects/project/main-resnet.py", line 707, in <module>
    results = train_net(net=net,
  File "C:/Users/User/PycharmProjects/project/main-resnet.py", line 495, in train_net
    train_auc = roc_auc_score(training_true, training_estimated)
  File "C:\Users\User\anaconda3\envs\project\lib\site-packages\sklearn\metrics\_ranking.py", line 566, in roc_auc_score
    y_true = label_binarize(y_true, classes=labels)[:, 0]
  File "C:\Users\User\anaconda3\envs\project\lib\site-packages\sklearn\preprocessing\_label.py", line 546, in label_binarize
    Y = sp.csr_matrix((data, indices, indptr), shape=(n_samples, n_classes))
  File "C:\Users\User\anaconda3\envs\project\lib\site-packages\scipy\sparse\compressed.py", line 66, in __init__
    idx_dtype = get_index_dtype((indices, indptr),
  File "C:\Users\User\anaconda3\envs\project\lib\site-packages\scipy\sparse\sputils.py", line 153, in get_index_dtype
    int32min = np.iinfo(np.int32).min
  File "C:\Users\User\anaconda3\envs\project\lib\site-packages\numpy\core\getlimits.py", line 651, in __init__
    self.dtype = numeric.dtype(type(int_type))
TypeError: 'NoneType' object is not callable
python-BaseException
问题原因

该问题是PyCharm 2022.1.x版本内置的pydevd调试器和numpy 1.23及以上版本的兼容性bug导致:调试器在做变量追踪、自动求值时会意外覆盖numpy内部模块的numeric.dtype引用,将其置为None,后续scipy、sklearn调用numpy的类型判断逻辑时,就会触发NoneType不可调用的错误。普通运行模式不会加载pydevd的调试注入逻辑,原生Python控制台也不会触发该注入,因此这两个场景下代码运行正常。

解决方法

任选以下一种方案即可修复:

  • 升级PyCharm到2022.2及更高版本,JetBrains已经在后续版本修复了pydevd污染numpy内部变量的问题
  • 暂时无法升级PyCharm的话,将当前虚拟环境的numpy版本降级到1.22.x系列,执行命令pip install numpy==1.22.4即可避开兼容性冲突
  • 不想改动依赖版本的话,可以关闭PyCharm调试器的自动求值功能:打开「设置-构建、执行、部署-Python调试器」,取消勾选「调试控制台自动显示变量值」「启用代码片段求值」选项,重启调试会话即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 03:06:23