python anytree_使用Anytree和graphviz在python中渲染树。无法打开文件

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

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

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.

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?

解决方案

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:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值