Skip to main content
 首页 » 编程设计

python之我如何确保 Graphviz 可执行文件在我系统的路径上

2025年05月04日21yyy_WW

我正在使用 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 

它执行时没有任何错误。

关于如何解决这个问题的任何想法?这让我发疯。谢谢!

请您参考如下方法:

我遵循了 @aprameyo roy 发布的解决方案这里 > "RuntimeError: Make sure the Graphviz executables are on your system's path" after installing Graphviz 2.38

所需的系统路径在我的 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/'