【问题标题】:subprocess call unable to find dot although it's installed尽管已安装子进程调用但无法找到点
【发布时间】:2021-05-08 11:16:50
【问题描述】:

我正在关注this article 提供的this sample code。 IDE 是 Spyder 4.1.5 和 Python 3.8,anaconda 得到以下异常“FileNotFoundError: [WinError 2] The system cannot find the file specified”。

我是 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>

【问题讨论】:

  • 它是否将 tree.dot 转换为 tree.png?
  • 是的。文件tree.dot 已生成;该错误发生在稍后尝试将其转换为 .png 的步骤中。

标签: python scikit-learn anaconda spyder


【解决方案1】:

如你所见,错误就在这一行

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

归结为

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

subprocess 模块中。所以似乎找不到executable,即dot。检查是否可以在 cmd 中运行 where dot。如果没有,那么您可能需要安装 graphviz 库。安装过程中一定要勾选“添加到路径”

【讨论】:

  • 我已经检查了环境,dot在路径中可用,并且包graphviz已经安装。
  • 在Anaconda Prompt中直接调用命令dot -Tpng tree.dot -o tree.png -Gdpi=600就可以了;只是子进程调用不起作用。
猜你喜欢
  • 1970-01-01
  • 2016-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-12
  • 1970-01-01
相关资源
最近更新 更多