在 Windows 10 上安装 Graphviz 以便与 Python 3 一起使用

Installing Graphviz for use with Python 3 on Windows 10

所以我 Python 3.6 在我的 Windows 10 计算机上已经有一段时间了,今天我刚刚下载并安装了 graphviz 0.8.2 (https://pypi.python.org/pypi/graphviz)通过管理命令行打包:

pip3 install graphviz

只有在这一点之后,我才下载了 Graphviz 2.38 MSI 安装程序文件并安装了程序:

C:\Program Files (x86)\Graphviz2.38

然后我尝试 运行 这个简单的 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.render('round-table.gv', view=True)

但不幸的是,当我尝试从命令行 运行 我的 Python 程序时收到以下错误:

Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\site-packages\graphviz\backend.py", line 124, in render
    subprocess.check_call(args, startupinfo=STARTUPINFO, stderr=stderr)
  File "C:\Program Files\Python36\lib\subprocess.py", line 286, in check_call
    retcode = call(*popenargs, **kwargs)
  File "C:\Program Files\Python36\lib\subprocess.py", line 267, in call
    with Popen(*popenargs, **kwargs) as p:
  File "C:\Program Files\Python36\lib\subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "C:\Program Files\Python36\lib\subprocess.py", line 997, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\foldername\testing.py", line 11, in <module>
    dot.render('round-table.gv', view=True)
  File "C:\Program Files\Python36\lib\site-packages\graphviz\files.py", line 176, in render
    rendered = backend.render(self._engine, self._format, filepath)
  File "C:\Program Files\Python36\lib\site-packages\graphviz\backend.py", line 127, in render
    raise ExecutableNotFound(args)
graphviz.backend.ExecutableNotFound: failed to execute ['dot', '-Tpdf', '-O', 'round-table.gv'], make sure the Graphviz executables are on your systems' PATH

请注意我问的问题与此处提出的问题非常相似: "RuntimeError: Make sure the Graphviz executables are on your system's path" after installing Graphviz 2.38

但由于某些原因,将这些路径(在上述 link 的解决方案中建议)添加到系统变量中不起作用,我不知道为什么!我也尝试在添加路径后重新启动计算机,但仍然没有成功。见下图:

虽然另一个建议的解决方案是在我的 Python 代码前添加这几行,确实 工作:

import os
os.environ["PATH"] += os.pathsep + 'C:/Program Files (x86)/Graphviz2.38/bin/'

但问题是:我不明白为什么添加到环境变量不起作用,这是我最关心的问题。 所以我的问题是:为什么在 Python 脚本前面添加这些代码行有效,但更改环境变量却没有? 我需要做什么才能将我的脚本设置为 运行 而无需在前面添加这些代码行?

请问post 设置 PATH 环境变量后在 cmd window 中键入 SET 时得到的输出?

是否包含C:/Program Files (x86)/Graphviz2.38/bin/

必须重新启动 cmd window 才能使更新的环境变量生效!