源代码参考【此博客】,运行在win10系统下
在最后一行输出pdf文件出了如下bug
graph[0].write_pdf("iris.pdf")
解决方法:
先确认已经安装Graphviz,安装过程不赘述
1、首先尝试在系统环境变量下将dot.exe路径添加进去,具体方法参见百度
但我添加后仍有此错误抛出,继续搜索尝试。。。
2、在pydot.py源文件中添加绝对路径,参考此博客思路,但此博客所对应的pydot版本比较低,已不适用于最新版pydot
定位到抛错行creat函数段L1869,我们看到有一段关于环境变量的代码
env = dict()
env['PATH'] = os.environ.get('PATH', '')
env['LD_LIBRARY_PATH'] = os.environ.get('LD_LIBRARY_PATH', '')
cmdline = [prog, '-T' + format] + args + [tmp_name]
try:
p = subprocess.Popen(
cmdline,
env=env,
cwd=tmp_dir,
shell=False,
stderr=subprocess.PIPE, stdout=subprocess.PIPE)
except OSError as e:
if e.errno == os.errno.ENOENT:
args = list(e.args)
args[1] = '"{prog}" not found in path.'.format(
prog=prog)
raise OSError(*args)
else:
raise
推测大概率是因为PATH找不到,我们再看creat函数的definition:
@param prog: either:
- name of GraphViz executable that
can be found in the `$PATH`, or
- absolute path to GraphViz executable.
If you have added GraphViz to the `$PATH` and
use its executables as installed
(without renaming any of them)
then their names are:
- `'dot'`
- `'twopi'`
- `'neato'`
- `'circo'`
- `'fdp'`
- `'sfdp'`
On Windows, these have the notorious ".exe" extension that,
only for the above strings, will be added automatically.
The `$PATH` is inherited from `os.env['PATH']` and
passed to `subprocess.Popen` using the `env` argument.
If you haven't added GraphViz to your `$PATH` on Windows,
then you may want to give the absolute path to the
executable (for example, to `dot.exe`) in `prog`.
那么只要在creat函数中添加绝对路径即可解决!
具体操作:
在L1710左右我们看到有一个set_prog函数,我在此函数中添加了path:
def set_prog(self, prog):
"""Sets the default program.
Sets the default program in charge of processing
the dot file into a graph.
"""
path = r'path/to/your/dot/exe/file'# 例如我的:F:\Program File\Anaconda\envs\python3.5\Lib\site-packages\bin
prog = os.path.join(path, prog)
prog += '.exe'
#self.prog = prog
return prog
回到creat函数,调用此函数:
if prog is None:
prog = self.prog
assert prog is not None
prog = self.set_prog('dot') #调用函数
再运行demo成功生成pdf文件
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)