安装Graphviz 2.38后出现“RuntimeError:请确保Graphviz可执行文件在您的系统路径上”的错误提示。

280

我下载了 Graphviz 2.38 MSI 版本并安装在文件夹 C:\Python34 下,然后运行了 pip install Graphviz,一切顺利。在系统路径中我添加了 C:\Python34\bin。当我尝试运行一个测试脚本时,在代码行 filename=dot.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安装,但仍然无效。

完整的回溯消息如下:

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

有人对此有经验吗?

38个回答

3
在Windows环境中,使用Chocolately进行安装,它会确保环境变量被正确设置。
choco install graphviz

这对我有用。请确保以管理员模式运行它。 - john k

3

在安装完该软件包之后(如果您尚未安装,请点击此处),将dot.exe的路径作为新的系统变量添加。

默认路径如下:

C:\Program Files (x86)\Graphviz2.38\bin\dot.exe

输入图像说明


我没有这个 dot.exe 文件。 - Maths12

3
我在Mac OS(El Capitan)上使用PyCharm IDE时遇到了相同的错误消息。按照RZK的建议,我使用brew安装了Graphviz,并使用PyCharm安装了graphviz python包(通过在终端中尝试dot -V并获得以下结果来检查Graphviz是否已正确安装:dot - graphviz version 2.40.1 (20161225.0304))。然而,当尝试从PyCharm调用Graphviz时,仍然会收到错误消息。
我不得不在PyCharm选项中添加路径/usr/local/bin,如this question的答案所建议,以解决问题。

2

graphviz 添加到系统路径中

  1. Windows - 编辑系统环境变量。
  2. 选择环境变量。
  3. 选择 Path - 新建
  4. 添加 graphviz 的路径

例如: C:\Users\AppData\Local\Continuum\anaconda3\Library\bin\graphviz


2

2

操作系统为Mojave 10.14,Python版本为3.6。

在终端中使用pip install graphviz命令反馈良好,但在Jupyter笔记本中制作图表时出现错误。随后我运行了brew install graphviz命令,在终端中出现了错误。然后我运行了conda install graphviz命令,图表成功制作。

根据@Leighton的评论:pip只能得到路径问题与你的相同,而conda只能得到导入错误。

注:Original Answer翻译成"最初的回答"


2

请在Anaconda提示符中逐个输入以下代码。

这对我有效。

来源:https://anaconda.org/conda-forge/python-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 

之后重新启动内核!


1

对于没有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变量中。我没有测试过这个方法。


如果 whereis dot 没有输出任何东西,只有 dot: 怎么办?我正在使用 Windows 上的 PyCharm 连接到 Ubuntu 16.04 服务器,似乎找不到必要的 bin 文件夹以添加 os.environ["PATH"] += os.pathsep + '...' - bit_scientist
请参考我在这里发布的问题。 - bit_scientist

1

1.从https://graphviz.gitlab.io/_pages/Download/Download_windows.html下载msi文件,安装Windows包。

2.将C:\Program Files (x86)\Graphviz2.38\bin添加到用户路径的环境变量中。

  1. 将C:\Program Files (x86)\Graphviz2.38\bin\dot.exe添加到系统路径中。

  2. 重新启动Python笔记本。

这样就可以了。


1
#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))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接