PyCharm中导入dsplot库失败及安装报错的原因与解决方法
问题
尝试使用dsplot库绘制树图,代码如下:
from dsplot.graph import Graph graph = Graph( {0: [1, 4, 5], 1: [3, 4], 2: [1], 3: [2, 4], 4: [], 5: []}, directed=True ) graph.plot()
运行代码时出现错误:
from dsplot.graph import Graph ModuleNotFoundError: No module named 'dsplot'
在PyCharm终端执行命令pip install dsplot尝试安装该库,出现新错误:
Collecting dsplot Using cached dsplot-0.9.0-py3-none-any.whl (8.8 kB) Collecting pygraphviz<2.0,>=1.7 Using cached pygraphviz-1.12.tar.gz (104 kB) ERROR: Error [WinError 3] The system cannot find the path specified while executing command pip subprocess to install build dependencies Installing build dependencies ... error ERROR: Could not install packages due to an OSError: [WinError 3] The system cannot find the path specified
请问该库安装失败的原因是什么,如何解决?
原因分析
dsplot依赖pygraphviz库,而pygraphviz在Windows系统下无法直接通过pip编译安装,核心原因有两点:
- 系统未安装Graphviz二进制文件,pip编译pygraphviz时找不到依赖路径,触发WinError 3
- Windows默认缺少C/C++编译环境,无法处理pygraphviz的源码编译流程
解决方法
方法一:先安装Graphviz再编译依赖
- 下载Graphviz的Windows安装包,安装时务必勾选Add Graphviz to the system PATH选项,确保系统能找到其二进制文件
- 重启PyCharm终端,执行
pip install pygraphviz,此时pip可找到Graphviz路径完成编译 - 最后执行
pip install dsplot即可完成安装
方法二:直接安装预编译的pygraphviz包
- 在PyCharm终端执行
pip install pygraphviz --only-binary :all:,跳过源码编译,直接安装预编译好的二进制包 - 完成后再执行
pip install dsplot
方法三:替换为无需编译的替代方案
如果上述方法仍无法解决,可改用networkx+matplotlib的组合实现相同的树图绘制功能,代码示例:
import networkx as nx import matplotlib.pyplot as plt # 构建有向图 G = nx.DiGraph({0: [1, 4, 5], 1: [3, 4], 2: [1], 3: [2, 4], 4: [], 5: []}) # 绘制图形 nx.draw(G, with_labels=True, node_color='lightblue', node_size=800) plt.show()
内容的提问来源于stack exchange,提问作者David777
相关产品推荐
相关产品推荐

