python - 尽管已安装子进程调用但无法找到点
问题描述
我正在关注本文提供的这个示例代码。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>
解决方案
如您所见,错误在该行
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库。确保在安装过程中选中“添加到路径”
推荐阅读
- kubernetes - 禁止:“system:serviceaccount:default:default”无法创建资源。如何添加权限?
- javascript - 从 YouTube Player API 向 iframe 添加 HTML 属性
- python - 在 Visual Studio Code 中编写 Python 时,下划线在 Docstrings 中使用反斜杠进行转义
- android - 重新创建活动时使用原始 Intent
- tomcat - 为 Tomcat 设置 Apache2
- delphi - 从已编译的可执行文件/包中提取 ProductVersion
- sql - 错误代码:1241。尝试子查询时,操作数应包含 1 列
- javascript - 在数组中获取单个 JSON 对象
- c# - 有没有办法从 c# 将一组 SafeHandles 发送到本机函数?
- google-bigquery - 根据 id 列的条目数查询以将表中的数据聚合到另一个表中