在Windows 10上安装Graphviz以与Python 3配合使用

vecaoik1  于 2023-03-31  发布在  Python
关注(0)|答案(2)|浏览(230)

所以我在Windows 10电脑上安装Python 3.6已经有一段时间了,今天我通过admin命令行下载并安装了graphviz 0.8.2https://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
但是由于某些原因,将这些路径(在上面的链接中的解决方案中建议)添加到系统变量不起作用,我不知道为什么!我也尝试在添加路径后重新启动计算机,仍然没有成功。请参见下图:

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

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

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

y1aodyip

y1aodyip1#

在设置PATH环境变量后,您可以发布在cmd窗口中键入SET时获得的输出吗?
是否包含C:/Program Files (x86)/Graphviz2.38/bin/
在更新的环境变量生效之前,必须重新启动cmd窗口!

hsgswve4

hsgswve42#

另外,请确保添加到SYSTEM路径变量,而不是当前用户路径变量。

相关问题