Python, not able to graph trees using graphviz with the anytree package

╄→尐↘猪︶ㄣ 提交于 2021-02-05 08:11:19

问题


So I have installed the anytree package (after great effort; I had to add some environment variables on my system). Having done this I can use almost all functionalities of the anytree package - just not the one I want. I wish to use graphviz in conjunction with the anytree package in order to graph trees using the 'DotExporter' command.

I have installed graphviz; its path is C:\Users\joel\Anaconda3_2\Lib\site-packages\graphviz, and I have also added the line of code 'from anytree.exporter import DotExporter' to my Python document (in addition to the other anytree imports needed to make trees).

Therefore, having defined the root node 'root' of my tree, I should now be able to produce pngs and dot files of the tree using either of the commands 'DotExporter(root).to_picture('root.png')', however I get the following error message:

'FileNotFoundError: [WinError 2] The system cannot find the file specified'.

Does anybody know what is going on? I am using Python 3.6.2 with the engine Spyder as a part of the Anaconda distribution (I have Anaconda 3.2). I am 99% certain I have the most recent versions of anytree and graphviz.

UPDATE: So I learned that there has been a big compatibility issue between Anaconda and graphviz for about a year now and assumed that this was the problem. After trying all the solutions people suggested I still wasn't able to get my program to work so I ended up uninstalling Anaconda and instead started using PyCharm together with the standard Python interpreter. However the program still wouldn't work, so even though there are compatibility issues between Anaconda and graphviz, it turns out that wasn't the problem. I tried the program on my housemate's machine which also uses Anaconda and got the same error I had been getting.

In the end I found the following workaround: I couldn't get the 'DotExporter(root).to_picture('root.png')' command to work, but the 'DotExporter(root).to_dotfile('root.dot')' command was working. '.dot' files contain code and can be opened in Notepad (other word processing softwares will work too). Therefore I just had to find a way to graph the tree using the dot file instead of using the 'DotExporter(root).to_picture('root.png')' command. The easiest way to do this is via either of the online tools http://www.webgraphviz.com/ and http://sandbox.kidstrythisathome.com/erdos/. Just open your dot file in Notepad, copy its contents and paste it into the websites and hit generate.


回答1:


Here is my workaround. I was working the examples of "getting started" with the following initial steps:

Create a tree object as udo and then trying DotExporter(udo).to_picture("udo.png"). This was where I got the same error as OP.

Assuming you have already installed graphviz and python-graphviz (e.g conda install -c anaconda graphviz python-graphviz) you can output the graph (first saving a dotfile) with this:

DotExporter(udo).to_dotfile('udo.dot')

from graphviz import Source
Source.from_file('udo.dot')

Then save image file with this:

from graphviz import render
render('dot', 'png', 'udo.dot') 


来源:https://stackoverflow.com/questions/51447235/python-not-able-to-graph-trees-using-graphviz-with-the-anytree-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!