graphviz.backend.execute.ExecutableNotFound: failed to execute WindowsPath(‘dot‘), make sure the Gra

目录

场景:

报错:

解决:

目录结构:

输入数据:

主函数: 

输出效果:


场景:

我在使用graphviz这个第三方库,python实现求两点间所有路径的算法 并使用 graphviz 图形化展示路径。

报错:

graphviz.backend.execute.ExecutableNotFound: failed to execute WindowsPath('dot'), make sure the Graphviz executables are on your systems' PATH

graphviz.backend.execute.ExecutableNotFound: failed to execute WindowsPath(‘dot‘), make sure the Gra_第1张图片

解决:

大家习惯pip install graphviz去安装,但是graphviz是个软件,不能单独用Pip安装。

(1)先将自己安装好的卸载

pip uninstall graphviz

graphviz.backend.execute.ExecutableNotFound: failed to execute WindowsPath(‘dot‘), make sure the Gra_第2张图片

(2)首先要重新安装这个第三方库,

pip install graphviz

(3)然后下载graphviz的安装包 ,网址: 

https://graphviz.org/download/

进入下面页面。此处我选择这个版本进行下载, 

graphviz.backend.execute.ExecutableNotFound: failed to execute WindowsPath(‘dot‘), make sure the Gra_第3张图片

(4)下载之后,点击下载成功的.exe文件(本文简称graphziv.exe)进行安装,一路默认即可,安装的时候记住安装路径(最好放到anaconda文件夹下,即**\Anaconda\Graphziv后续配置环境变量的时候要使用) ,

graphviz.backend.execute.ExecutableNotFound: failed to execute WindowsPath(‘dot‘), make sure the Gra_第4张图片

在安装过程中,一定要注意:将之加入到环境变量中去

(5)如果没有加入请参考此步骤进行添加环境变量,

1.此电脑右键----->属性----->高级系统设置----->环境变量----->xxx的用户变量u----->path----->新建

粘贴文件的安装路径+\bin :例如D:\python\graphviz\bin

graphviz.backend.execute.ExecutableNotFound: failed to execute WindowsPath(‘dot‘), make sure the Gra_第5张图片

graphviz.backend.execute.ExecutableNotFound: failed to execute WindowsPath(‘dot‘), make sure the Gra_第6张图片

2. 此电脑右键----->属性----->高级系统设置----->环境变量----->系统环境变量(s)----->path----->新建
粘贴文件的安装路径+\bin\dot.exe :例如D:\python\graphviz\bin\dot.exe

graphviz.backend.execute.ExecutableNotFound: failed to execute WindowsPath(‘dot‘), make sure the Gra_第7张图片

graphviz.backend.execute.ExecutableNotFound: failed to execute WindowsPath(‘dot‘), make sure the Gra_第8张图片

 (6)运行我的py代码,

目录结构:

 其中,

输入数据:

graph.txt:

代码应能读取规定格式的TXT文档作为输入,格式如下:

  • 第一行:图的节点数N,边数V

  • 后续V行: 图中每一条边的起点、终点

  • 最后一行:待求解目标的起点、终点

16 20
1 2
1 5
2 3
2 4
3 4
3 5
4 5
5 7
5 14
5 6
6 9
7 4
9 4
10 6
11 5
12 7
13 8
13 6
15 13
16 10
2 5

主函数: 

#!/usr/bin/env python3
#find_all_routes_of_2_points.py
#fumiama 20201001
import sys
from graphviz import Digraph

def exitWithError(*error):
    print(*error)
    exit()

def printGraph2Pdf(grf): grf.render('graph-output/output.gv', view=True)

def printRoute(stackList):
    global nodeNumber
    nodeNumber += 1
    dot.node(str(nodeNumber), stackList[0])
    for node in stackList[1:]:
        nodeNumber += 1
        dot.node(str(nodeNumber), node)
        dot.edge(str(nodeNumber-1), str(nodeNumber))

def addEdge(a, b):
    global edgeLinks
    if a not in edgeLinks: edgeLinks[a] = set()
    if b not in edgeLinks: edgeLinks[b] = set()
    edgeLinks[a].add(b)
    edgeLinks[b].add(a)

def loadGraph(fileName):
    try: f = open(fileName, 'r')
    except: exitWithError("打开文件失败, 请检查文件名是否正确或程序是否有权限访问")
    global size, edgeLinks
    size, edgeCount = map(int, f.readline().split())
    print("节点:", size, "边数:", edgeCount)
    for i in range(1, size+1): dot.node(str(i), str(i))
    for i in range(edgeCount):
        a, b = f.readline().split()
        addEdge(a, b)
        dot.edge(a, b)
    re = f.readline()
    f.close()
    return re

def findAllRoutes(start, end):
    global edgeLinks, stack
    stack.append(start)
    if start == end:
        print("找到路径:", stack)
        printRoute(stack)
        stack.pop()
    else:
        for nextPoint in edgeLinks[start]:
            if nextPoint not in stack: findAllRoutes(nextPoint, end)
        stack.pop()

def rmRoute2Itself(start):
    for point in edgeLinks:
        if point != start and start in edgeLinks[point]:
            edgeLinks[point].remove(start)

if __name__ == '__main__':

    dot = Digraph(comment='Gragh2Print')
    dot.edge_attr.update(arrowhead='none')
    dot.graph_attr['rankdir'] = 'LR'

    edgeLinks = dict()
    size = 0

    stack = []
    nodeNumber = 0

    # sys = "D:find_all_routes_of_2_points.py pdf graph.txt"
    # if len(sys.argv) != 3: exitWithError("用法:", sys.argv[0], "[pdf|nopdf] 文件位置")
    a, b = loadGraph("graph.txt").split()
    print("起点:", a, "终点:", b)
    rmRoute2Itself(a)
    nodeNumber = size + 1
    findAllRoutes(a, b)
    print("生成pdf格式图形化报告...")
    printGraph2Pdf(dot)

就可以看到最后生成的结果了,

输出效果:

节点: 16 边数: 20
起点: 2 终点: 5
找到路径: ['2', '3', '5']
找到路径: ['2', '3', '4', '5']
找到路径: ['2', '3', '4', '7', '5']
找到路径: ['2', '3', '4', '9', '6', '5']
找到路径: ['2', '4', '5']
找到路径: ['2', '4', '7', '5']
找到路径: ['2', '4', '9', '6', '5']
找到路径: ['2', '4', '3', '5']
找到路径: ['2', '1', '5']
生成pdf格式图形化报告...

并且生成pdf文件, 

graphviz.backend.execute.ExecutableNotFound: failed to execute WindowsPath(‘dot‘), make sure the Gra_第9张图片

 打开pdf文件显示为:

graphviz.backend.execute.ExecutableNotFound: failed to execute WindowsPath(‘dot‘), make sure the Gra_第10张图片

ok,问题解决。

你可能感兴趣的:(#,python,python,开发语言)