'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'
Solution 1:[1]
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
.
Solution 2:[2]
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"
Solution 3:[3]
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
.
Solution 4:[4]
I solved this issue in a different way:
- Open graphviz file on my PC through following path (Path may differ) "C:\Users\Anoop\anaconda3\Lib\site-packages\graphviz\backend"
- Copy the encoding.py file from here
- Paste this file in the backend "C:\Users\Anoop\anaconda3\Lib\site-packages\graphviz\backend"
- Problem solved
Solution 5:[5]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | macserv |
Solution 2 | |
Solution 3 | michaelosthege |
Solution 4 | Anoop Dixit |
Solution 5 | maurich17 |