首页 > 解决方案 > 尽管已安装子进程调用但无法找到点

问题描述

我正在关注本文提供这个示例代码。IDE 是 Spyder 4.1.5 和 Python 3.8,anaconda 得到以下异常“FileNotFoundError:[WinError 2] 系统找不到指定的文件”。

我是 python(和 Spyder)的新手,所以不确定缺少什么文件,因为异常消息不包含文件名。任何提示将不胜感激。

我已经检查了环境,dot在路径中可用,并且graphviz已经安装了包。

异常跟踪:

runfile('C:/my/work/smlb/challenge_1/code/tree_to_image.py', wdir='C:/my/work/smlb/challenge_1/code')
Traceback (most recent call last):

  File "C:\my\work\smlb\challenge_1\code\tree_to_image.py", line 31, in <module>
    call(['dot', '-Tpng', 'tree.dot', '-o', 'tree.png', '-Gdpi=600'])

  File "C:\Users\user\anaconda3\lib\subprocess.py", line 340, in call
    with Popen(*popenargs, **kwargs) as p:

  File "C:\Users\user\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 105, in __init__
    super(SubprocessPopen, self).__init__(*args, **kwargs)

  File "C:\Users\user\anaconda3\lib\subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,

  File "C:\Users\user\anaconda3\lib\subprocess.py", line 1307, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,

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

我试图运行的示例代码:

我正在尝试运行以下示例源代码来生成图像以可视化二元决策。

from sklearn.datasets import load_iris
iris = load_iris()

# Model (can also use single decision tree)
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(n_estimators=10)

# Train
model.fit(iris.data, iris.target)
# Extract single tree
estimator = model.estimators_[5]

from sklearn.tree import export_graphviz
# Export as dot file
export_graphviz(estimator, out_file='tree.dot', 
                feature_names = iris.feature_names,
                class_names = iris.target_names,
                rounded = True, proportion = False, 
                precision = 2, filled = True)

# Convert to png using system command (requires Graphviz)
from subprocess import call
call(['dot', '-Tpng', 'tree.dot', '-o', 'tree.png', '-Gdpi=600'])
#
# It works if directly call command `dot -Tpng tree.dot -o tree.png -Gdpi=600`
# but the subprocess call here doesn't work
#

# Display in jupyter notebook
from IPython.display import Image
Image(filename = 'tree.png')

在 Anaconda Prompt 中检查 python 包:

(base) C:\Users\user>conda list graphviz
# packages in environment at C:\Users\user\anaconda3:
#
# Name                    Version                   Build  Channel
graphviz                  2.38                 hfd603c8_2
python-graphviz           0.16               pyhd3eb1b0_1

(base) C:\Users\user>where dot
C:\Users\user\anaconda3\Library\bin\dot.bat

(base) C:\Users\user>

标签: pythonscikit-learnanacondaspyder

解决方案


如您所见,错误在该行

call(['dot', '-Tpng', 'tree.dot', '-o', 'tree.png', '-Gdpi=600'])

归结为

hp, ht, pid, tid = _winapi.CreateProcess(executable, args,

subprocess模块中。因此,似乎无法找到executable, ie 。dot检查是否可以where dot在cmd中运行。如果没有,那么您可能需要安装graphviz库。确保在安装过程中选中“添加到路径”


推荐阅读