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

seaborn clustermap报错:已安装Scipy仍提示需其可用

解决seaborn clustermap检测不到已安装scipy的问题

问题描述

已在venv虚拟环境中安装并导入scipy,但运行seaborn的clustermap函数时仍报错,提示需要scipy环境。

运行代码:

import seaborn as sn
import scipy
sn.clustermap(data)

错误信息:

venv\lib\site-packages\seaborn\matrix.py:1250, in clustermap(data, pivot_kws, method, metric, z_score, standard_scale, figsize, cbar_kws, row_cluster, col_cluster, row_linkage, col_linkage, row_colors, col_colors, mask, dendrogram_ratio, colors_ratio, cbar_pos, tree_kws, **kwargs)
   1157 """
   1158 Plot a matrix dataset as a hierarchically-clustered heatmap.
   1159 
   (...)
   1247 
   1248 """
   1249 if _no_scipy:
-> 1250     raise RuntimeError("clustermap requires scipy to be available")
   1252 plotter = ClusterGrid(data, pivot_kws=pivot_kws, figsize=figsize,
   1253                       row_colors=row_colors, col_colors=col_colors,
   1254                       z_score=z_score, standard_scale=standard_scale,
   1255                       mask=mask, dendrogram_ratio=dendrogram_ratio,
   1256                       colors_ratio=colors_ratio, cbar_pos=cbar_pos)
   1258 return plotter.plot(metric=metric, method=method,
   1259                     colorbar_kws=cbar_kws,
   1260                     row_cluster=row_cluster, col_cluster=col_cluster,
   1261                     row_linkage=row_linkage, col_linkage=col_linkage,
   1262                     tree_kws=tree_kws, **kwargs)

RuntimeError: clustermap requires scipy to be available

排查与解决步骤

  • 确认Notebook使用的虚拟环境
    Notebook可能默认使用全局Python环境而非你的venv。运行以下代码检查当前环境路径:

    import sys
    print(sys.executable)
    

    正常输出应为venv\Scripts\python.exe(Windows)或venv/bin/python(Linux/macOS)。若不符,需切换Notebook内核到目标虚拟环境。

  • 重新安装scipy到正确环境
    激活venv后执行安装命令:

    • Windows:
      venv\Scripts\activate
      pip install scipy
      
    • Linux/macOS:
      source venv/bin/activate
      pip install scipy
      
  • 检查版本兼容性
    部分旧版seaborn对scipy版本有要求,查看当前版本:

    import seaborn as sn
    import scipy
    print("seaborn版本:", sn.__version__)
    print("scipy版本:", scipy.__version__)
    

    若版本不兼容,升级对应包:

    pip install --upgrade seaborn scipy
    
  • 验证scipy核心模块可用性
    seaborn的clustermap依赖scipy.cluster.hierarchy,手动测试导入:

    try:
        from scipy.cluster import hierarchy
        print("scipy.cluster.hierarchy导入成功")
    except ImportError as e:
        print("导入失败:", e)
    

    若导入失败,说明scipy安装不完整,需重新安装,或解决系统层面的编译依赖缺失问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 16:45:33