Solution: GraphViz Installation Error Python

Correctly installing GraphViz for python is a painstaking task. The reason is installation and PATH related issues while using pip. The problem intensifies if you are working on your work-related device that has restrictions with respect to installation and file/folder access. Thus, despite several resources on the internet, many of them do not stand applicable for your case. I had to spend a significant amount of time searching for a solution that works. Most of the solutions I found over the internet were either not applicable to my case (owing to restricted access on my work device) or they were written in a complex way to shroud the approach to be adopted or they did not work. In a nutshell, I did not find any lucid explanation, at least at could directly be followed until resolution by someone who is not a computer scientist or very code-savvy. Hence, to save time and the frustration coupled with it, I am sharing a simple method that resolved the issue in my case. 

For your references, while trying to visualize a tree (that was generated in a dot or txt file), I was having "GraphViz executables not found..." or "dot command is not valid batch internal or external command..." and similar related issues.

The following methods resolved the issue,

1. Do not use "pip" method as it didn't work

2. The version of graphiViz that worked was 2.38

3. To get this version, visit Index of /Archive/stable/windows (graphviz.org) and download graphviz-2.38.zip

4. Next extract the zip and copy the folder to path C or D. For example, in my device Anaconda3 and other packages were available at C:/Users/SiddharthaXXXX

5. run the following in jupyter (to test it in jupyter), i.e.

        import os

        os.environ["PATH"] += os.pathsep + 'C:/Users/SiddharthaXXXX/graphvviz-2.38/graphviz'

Note that this path basically is of the "dot.exe" file. In case, inside graphviz folder, there are further sub-folders before dot.exe is available, edit the path accordingly

6. Now you are ready to run the test

7. To test if everything is correct, lets try this code

        from sklearn import tree

        from sklearn.datasets import load_iris

        clf = tree.DecisionTreeClassifier()

        iris = load_iris()

        clf.fit(iris.data, iris.target)

        tree.export_graphviz(clf,out_file='tree.txt')

        import os

        os.environ["PATH"] += os.pathsep + 'C:/Users/SiddharthaXXXX/graphvviz-2.38/graphviz'

        os.system('dot -Tpng tree.txt -o tree.png')

8. This should read the generated tree.txt and convert it to tree.png


Comments