使用Anytree和graphviz在python中渲染树.无法打开文件 [英] Rendering a tree in python with Anytree and graphviz. Can't open file

查看:408
本文介绍了使用Anytree和graphviz在python中渲染树.无法打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题基于塔伦(Tarun)的答案,该答案是使用anytree和graphviz在python中渲染树: https://stackoverflow.com/a/49442969/2139007

This question is based on Tarun's Answer for rendering a tree in python using anytree and graphviz: https://stackoverflow.com/a/49442969/2139007

安装graphviz并将其添加到我的PATH变量后,我试图运行以下python代码:

After installing graphviz and adding it to my PATH variables i'am trying to run the following python code:

DotExporter(nodes[0]["a"],
        nodeattrfunc=lambda node: 'label="{}"'.format(node.display_name)).to_picture("tree.png")

上面的代码生成以下错误:

The above code generates the following error:

  Error: dot: can't open C:\Users\username\AppData\Local\Temp\tmpa7t554le
Traceback (most recent call last):
  File "D:\mypath\tree.py", line 34, in <module>
    dot.to_picture('tree.png')
  File "C:\Users\username\AppData\Local\Programs\Python\Python36-32\lib\site-packages\anytree\exporter\dotexporter.py", line 229, in to_picture
    check_call(cmd)
  File "C:\Users\username\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 291, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['dot', 'C:\\Users\\username\\AppData\\Local\\Temp\\tmpa7t554le', '-T', 'png', '-o', 'tree.png']' returned non-zero exit status 2.

如果在生成 .dot 后从控制台手动运行点命令' dot tree.dot -T png -o tree.png '不会发生此问题>文件

The problem does not occure if I run the dot command 'dot tree.dot -T png -o tree.png' manually from the console after generating a .dot file

DotExporter(nodes[0]["a"],
        nodeattrfunc=lambda node: 'label="{}"'.format(node.display_name)).to_dotfile('tree.dot')

有没有办法解决这个问题?

Is there a way to solve this?

推荐答案

我只是遇到了这个问题.经过2个小时的调试,我发现在Windows下,必须在dotexporter.py中将delete标志设置为False.转到"dotexporter.py"中的第224行,并进行以下更改.似乎是源文件中的错误.这对我有用:

I just ran into this problem. After 2 hours of debugging, I found out for Windows, you have to set the delete flag to False in dotexporter.py. Go to line 224 in "dotexporter.py" and make the following change. It seems like a bug in the source file. This worked for me:

 with NamedTemporaryFile("wb", delete=False) as dotfile:

这篇关于使用Anytree和graphviz在python中渲染树.无法打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆