Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to resolve AttributeError: module 'graphviz.backend' has no attribute 'ENCODING'

I am not sure why I get an AttributeError: module 'graphviz.backend' has no attribute 'ENCODING' when I tried to export regression tree to graphviz. I tried re-installing graphviz and sklearn but it doesn't solve the problem. Appreciate any advice on this.


AttributeError                            Traceback (most recent call last)
<ipython-input-4-9d9e0becf9b6> in <module>
      3 # graphviz is the drawing tool
      4 from sklearn.tree import export_graphviz
----> 5 import graphviz
      6 dot_data = export_graphviz(
      7     model,

C:\ProgramData\Anaconda3\lib\site-packages\graphviz\__init__.py in <module>
     25 """
     26 
---> 27 from .dot import Graph, Digraph
     28 from .files import Source
     29 from .lang import escape, nohtml

C:\ProgramData\Anaconda3\lib\site-packages\graphviz\dot.py in <module>
     30 
     31 from . import backend
---> 32 from . import files
     33 from . import lang
     34 

C:\ProgramData\Anaconda3\lib\site-packages\graphviz\files.py in <module>
     20 
     21 
---> 22 class Base(object):
     23 
     24     _engine = 'dot'

C:\ProgramData\Anaconda3\lib\site-packages\graphviz\files.py in Base()
     26     _format = 'pdf'
     27 
---> 28     _encoding = backend.ENCODING
     29 
     30     @property

AttributeError: module 'graphviz.backend' has no attribute 'ENCODING'
like image 623
Rayner Avatar asked Sep 05 '25 03:09

Rayner


1 Answers

I had a similar issue when using pipdeptree. It would seem that there was a very recent change to graphviz, intended to obfuscate its internals. Quoting the module author's reply in issue #149 (a similar issue with backend.FORMATS):

Submodules of graphviz are not part of the public API (cf. https://graphviz.readthedocs.io/en/stable/api.html). Please stick to the documented interface and use graphviz.FORMATS, see https://graphviz.readthedocs.io/en/stable/api.html#graphviz.FORMATS).

In the short term, you could downgrade your graphviz module… it looks like 0.18 was the last tag before the submodules were made opaque.

Moving forward, you may wish to create an issue and/or pull request against the sklearn-pandas repository, to replace graphviz.backend.FORMATS with graphviz.FORMATS, or even just cap its graphviz dependency at 0.18.

like image 178
macserv Avatar answered Sep 09 '25 19:09

macserv