4

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'
albert
  • 8,285
  • 3
  • 19
  • 32
Rayner
  • 41
  • 1
  • 1
  • 2

7 Answers7

4

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.

macserv
  • 3,546
  • 1
  • 26
  • 36
  • Note that the traceback indicates that the error is in the `graphviz` Python code itself. However I couldn't find this `files.py` in https://github.com/xflr6/graphviz so I'm really confused. – michaelosthege Dec 02 '21 at 19:40
  • thanks very much for this lead @macserv! downgrading worked for me too. – rikb Jan 03 '22 at 02:00
  • How can I downgrade the graphviz package? I am using anaconda – mblume Jul 06 '22 at 15:41
3

I had the same issue and I am very new to Python/conda world, so this might help newbies like me...

I downloaded graphviz 0.19.1 from: https://pypi.org/project/graphviz/#files

Source Distribution: graphviz-0.19.1.zip (247.8 kB view hashes) download link

and replaced graphviz folder with this version in "C:\Users\Nino\anaconda3\Lib\site-packages" (will be different for you) and rename it so that name is again graphviz.

"C:\Users\Nino\anaconda3\Lib\site-packages\graphviz"

Bulo
  • 31
  • 3
2

I have met same problem! The most voted answer works for me! And paste the code to forceably downgrade the graphviz.

pip install --force-reinstall graphviz==0.18
0

I had the same error with python-graphviz==0.16. OP did not include a version number, but it looks like the line numbers in the traceback match with v0.16.

Note that the traceback shows the error to be inside the python-graphviz package, so it's more likely that it's an issue with a dependency.

With python-graphviz==0.19 I don't get the import error.

On a side note: Versions shown by conda list or pip list can be misleading. In case of doubt check the content of the __init__.py.

michaelosthege
  • 621
  • 5
  • 15
0

I solved this issue in a different way:

  1. Open graphviz file on my PC through following path (Path may differ) "C:\Users\Anoop\anaconda3\Lib\site-packages\graphviz\backend"
  2. Copy the encoding.py file from here
  3. Paste this file in the backend "C:\Users\Anoop\anaconda3\Lib\site-packages\graphviz\backend"
  4. Problem solved
0

In my case, it seems that the class Base in the "C:\ProgramData\Anaconda3\lib\site-packages\graphviz\files.py" takes the 'backend folder' instead of the 'backend.py' on init.

quick solve: go to "C:\ProgramData\Anaconda3\lib\site-packages\graphviz" and rename the 'backend folder' to something else.

PS: since I didn't check the whole code, it may then cause another dependency problem.

0

Thanks! ver0.2 was trouble some and this error disappear after downgrading it with ver 0.19

henry
  • 11
  • To say thanks to the other users for the answers that helped you, please use comments instead of adding it as a separate answer. – ph0enix Jul 07 '22 at 13:04
  • 1
    @ph0enix You miss that this user cannot comment yet ( https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead ) and that they did not even taket he [tour] in which they could have learned things like this. If you can link to an existing answer, which this one is saying thanks to, then do so in a flag to bring this inappropriate post to the attention of the moderators. – Yunnosch Sep 18 '22 at 07:45