我下载了Graphviz 2.38 MSI版本,安装在C:\Python34文件夹下,然后运行pip安装Graphviz,一切顺利。在系统路径中我添加了C:\Python34\bin。当我试图运行一个测试脚本,在行filename= .render(filename='test'),我得到了一个消息

 RuntimeError: failed to execute ['dot', '-Tpdf', '-O', 'test'], make sure the Graphviz executables are on your systems' path

我尝试将“C:\Python34\bin\dot.exe”放在系统路径中,但它不起作用,我甚至创建了一个新的环境变量“GRAPHVIZ_DOT”,其值为“C:\Python34\bin\dot.exe”,仍然不起作用。我尝试卸载Graphviz和pip uninstall Graphviz,然后重新安装它和pip install,但没有任何工作。

整个回溯消息是:

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\graphviz\files.py", line 220, in render
    proc = subprocess.Popen(cmd, startupinfo=STARTUPINFO)
  File "C:\Python34\lib\subprocess.py", line 859, in __init__
    restore_signals, start_new_session)
  File "C:\Python34\lib\subprocess.py", line 1112, 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 "C:\Users\Documents\Kissmetrics\curves and lines\eventNodes.py", line 56, in <module>
    filename=dot.render(filename='test')
  File "C:\Python34\lib\site-packages\graphviz\files.py", line 225, in render
    'are on your systems\' path' % cmd)
RuntimeError: failed to execute ['dot', '-Tpdf', '-O', 'test'], make sure the Graphviz executables are on your systems' path

有人有经验吗?


当前回答

Mac & Big Sur。Python 3.8.6 w/vs code。虽然它应该包含在图表包中,但我不得不手动安装graphviz。

(mymltools) ➜  infrastructure git:(master) pip list
Package    Version
---------- -------
diagrams   0.18.0
graphviz   0.13.2
Jinja2     2.11.2
MarkupSafe 1.1.1
pip        20.3.2
setuptools 51.0.0
wheel      0.36.2

运行图表失败。然后手动运行

pipenv install graphviz

效果非常好。

其他回答

试着用python来做这个 导入系统 conda install——yes——prefix {sys. exe前缀}graphviz 进口graphviz

对于没有root权限的Linux用户,因此不能像其他答案中建议的那样使用sudo命令…

首先,激活你的conda虚拟环境(如果你想使用的话):

source activate virtual-env-name

然后安装graphviz,即使你已经使用pip完成了:

conda install graphviz

然后复制以下命令的结果:

whereis dot

在我的例子中,它的输出是:

/home/nader/anaconda2/bin/dot

并将其添加到PATH变量中。只需运行下面的命令

nano ~/.bashrc

并在打开的文件的末尾添加这些行:

PATH="/home/username/anaconda2/bin/dot:$PATH"
export PATH

现在按Ctrl+O,然后按Ctrl+X保存并退出。

问题现在应该解决了。

Pycharm用户请注意:Pycharm并不总是看到与您的终端相同的PATH变量。此解决方案不适用于Pycharm或其他ide。但是你可以通过添加这行代码来修复这个问题:

os.environ["PATH"] += os.pathsep + '/home/nader/anaconda2/bin'

到你的python程序。别忘了

import os

第一:)

编辑:如果你不想使用conda,你仍然可以从这里安装graphviz,不需要任何根权限,并将bin文件夹添加到PATH变量中。我没有测试这个。

你可以在Anaconda提示符中使用以下命令:

Conda Install python-graphviz

确保在Anaconda提示符中安装pm4py:

PIP安装pm4py

最后,你必须尝试使用Anaconda提示符运行你的python代码。

#Write this on anaconda prompt in admin mode
conda install -c anaconda graphviz
conda install -c conda-forge python-graphviz
conda install -c conda-forge/label/broken python-graphviz
conda install -c conda-forge/label/cf201901 python-graphviz
conda install -c conda-forge/label/cf202003 python-graphviz

#check dot -v in window's cmd prompt
C:\WINDOWS\system32>dot -V
dot - graphviz version 2.38.0 (20140413.2041)
(this means graphviz installed successfully)

#Add path to sys and user eve variables
PATH
C:\Anaconda3\pkgs\graphviz-2.38-hfd603c8_2\Library\bin
(search bin folder of graphviz and then copy n paste path in env variables)

#Re-run all cmds in jyupter notebook
#if error occurs (less chances)
#then 
#Restart anaconda and again run all cmds in jyupter notebook
eg.
import graphviz as gp
with open("tree.dot") as f:
    dot_read=f.read()
display(gp.Source(dot_read))

在Windows环境下,使用巧克力安装,它将确保环境变量设置正确。

choco install graphviz