首页 > 解决方案 > 如何确保 Graphviz 可执行文件位于我系统的 PATH 中?

问题描述

我正在使用 Python 3 在 Sublime Text 3 上使用 Graphviz。当我运行此代码时:

data = tree.export_graphviz(dtGini[55], out_file = None)
graph = graphviz.Source(data)
graph.render("testingthis")

我收到这些错误:

FileNotFoundError: [WinError 2] The system cannot find the file specified

During handling of the above exception, another exception occurred:

graphviz.backend.ExecutableNotFound: failed to execute ['dot', '-Tpdf', '-O', 'testingthis'], make sure the Graphviz executables are on your systems' PATH

看起来它找不到它需要的文件。在 Sublime Text 3 中,我对 Conda 的用户设置是:

{
// executable is the path to anaconda's python
// this python executable is used in order to find conda
"executable": "C:/ProgramData/Miniconda3/python.exe",

// Directory in which the conda envs are stored
// Default location is the user's home directory
"environment_directory": "C:/ProgramData/Miniconda3/envs",

// configuration is the path to conda's configuration file
"configuration": "~/.condarc"
}

我的控制面板中有这些环境变量:

C:\ProgramData\Miniconda3\Scripts\
C:\ProgramData\Miniconda3\
C:\ProgramData\Miniconda3\conda-meta\history
C:\Users\X\AppData\Local\conda\conda\pkgs
C:\Users\X\AppData\Local\conda\conda\pkgs\graphviz-2.38-hfd603c8_2\Library\bin
C:\Users\X\AppData\Local\conda\conda\pkgs\graphviz-2.38-hfd603c8_2\Library\bin\dot.exe

在 Anaconda 提示符中,当我输入 Python 时,按回车键,然后输入“import graphviz”,我没有收到任何错误。在 Sublime Text 3 中,如果我只有一个像 graph.py 这样的文件

import graphviz

它执行没有任何错误。

关于如何解决这个问题的任何想法?它快把我逼疯了。谢谢!

标签: pythonscikit-learnanacondagraphviz

解决方案


在安装 Graphviz 2.38 后,我遵循了@aprameyo roy在此处发布的解决方案 > “RuntimeError:确保 Graphviz 可执行文件位于系统路径上”

所需的系统路径在我的 PC 上进行了一些查找——我使用 Ananconda 安装了 graphviz 包。

将这两个命令添加到我的 jupyter notebook 解决了这个问题 - 将 C:/ 地址更改为您的安装位置:

(PS。我认为你需要在每次内核重启后重新运行它。)

# extra step to allow graphviz to be found 
import os
os.environ["PATH"] += os.pathsep + 'C:/Users/jed/Anaconda3/envs/keras/Library/bin/graphviz/'

推荐阅读