python graphviz安装
1.安装环境
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
WIN 10
2.安装软件
- graphviz-install-2.44.1-win64
- graphviz 0.15 python包
3.安装步骤
3.1 安装graphviz
- 根据电脑配置,下载graphviz软件(64位(推荐使用)/32位):
32-bit: https://www2.graphviz.org/Packages/stable/windows/10/cmake/Release/Win32/
64-bit: https://www2.graphviz.org/Packages/stable/windows/10/cmake/Release/x64/ - 运行.exe文件,安装软件
安装过程中需要注意在下图选项中选择Select Add Graphviz to the system PATH for current user。
其他步骤就是常规的安装了。
- 以管理员身份运行CMD,在CMD中输入“dot -c”
如果出现如下错误,可能是由于没有以管理员身份运行,请重试。
- CMD中输入“dot -v”发现可以正常显示信息。
- 重启需要使用Graphviz的应用。如果存在一些错误,尝试重启电脑。
3.2 安装graphviz 0.15
直接通过pip安装graphviz 0.15。
我这里已经安装成功,不再重新安装了。
4.graphviz起步-- python小栗子
from graphviz import Digraph
dot = Digraph(comment='The Round Table')
dot.node('A', 'King Arthur')
dot.node('B', 'Sir Bedevere the Wise')
dot.node('L', 'Sir Lancelot the Brave')
dot.edges(['AB', 'AL'])
dot.edge('B', 'L', constraint='false')
在控制台中输入:dot
生成图片如下:
OK。graphviz已经安装并在python中成功运行了。
5.常见错误解决
如果出现上图问题,可检查环境变量–用户变量中的path,是否存在“C:\Program Files\Graphviz 2.44.1\bin”。
若无则添加(只需要在用户变量的path添加即可),重启python后重新运行程序,应该就可以了。
参考文献
[1] https://forum.graphviz.org/t/new-simplified-installation-procedure-on-windows/224#installation-instructions-with-detailed-guidance-recommended-for-users-without-installation-experience
[2] http://www.graphviz.org/
[3] https://github.com/xflr6/graphviz/blob/master/examples/notebook.ipynb