pydot and graphviz error: Couldn’t import dot_parser, loading of dot files will not be possible

Posted on

Question :

pydot and graphviz error: Couldn’t import dot_parser, loading of dot files will not be possible

When I run a very simple code with pydot

import pydot
graph = pydot.Dot(graph_type='graph')

for i in range(3):

  edge = pydot.Edge("king", "lord%d" % i)
  graph.add_edge(edge)

vassal_num = 0
for i in range(3):
  for j in range(2):
    edge = pydot.Edge("lord%d" % i, "vassal%d" % vassal_num)
    graph.add_edge(edge)
    vassal_num += 1

graph.write_png('example1_graph.png')

It prints me the error message:

Couldn't import dot_parser, loading of dot files will not be possible.

I’m using python 2.7.3

Asked By: Sadik

||

Answer #1:

Answer for pydot >= 1.1:

The incompatibility of (upstream) pydot has been fixed by 6dff94b3f1, and thus pydot >= 1.1 will be compatible with pyparsing >= 1.5.7.


Answer applicable to pydot <= 1.0.28:

For anyone else who comes across this, it is due to the changes in pyparsing from 1.x to the 2.x release.
To install pydot using pip, first install the older version of pyparsing:

pip install pyparsing==1.5.7
pip install pydot==1.0.28

If you did not install pyparsing using pip, but instead used setup.py, then have a look at this solution to uninstall the package. Thanks @qtips.

Answered By: Sadik

Answer #2:

There is a new package in the pip repo called pydot2 that functions correctly with pyparsing2. I couldn’t downgrade my packages because matplotlib depends on the newer pyparsing package.

Note: python2.7 from macports

Answered By: Jonathan

Answer #3:

pydot used a private module variable (_noncomma) from pyparsing. The below diff fixes it to use for pyparsing 2.0.1:

diff --git a/dot_parser.py b/dot_parser.py
index dedd61a..138d152 100644
--- a/dot_parser.py
+++ b/dot_parser.py
@@ -25,8 +25,9 @@ from pyparsing import __version__ as pyparsing_version
 from pyparsing import ( nestedExpr, Literal, CaselessLiteral, Word, Upcase, OneOrMore, ZeroOrMore,
     Forward, NotAny, delimitedList, oneOf, Group, Optional, Combine, alphas, nums,
     restOfLine, cStyleComment, nums, alphanums, printables, empty, quotedString,
-    ParseException, ParseResults, CharsNotIn, _noncomma, dblQuotedString, QuotedString, ParserElement )
+    ParseException, ParseResults, CharsNotIn, dblQuotedString, QuotedString, ParserElement )

+_noncomma = "".join( [ c for c in printables if c != "," ] )

 class P_AttrList:
Answered By: Dana the Sane

Answer #4:

I forked the pydot repository [1], applied the Gabi Davar patch and some changes to support python-3. The package is available in the PyPI [2].

Cheers

Answered By: Gabi Davar

Answer #5:

$ sudo pip uninstall pydot

$ sudo pip install pydot2

See the following link: http://infidea.net/troubleshooting-couldnt-import-dot_parser-loading-of-dot-files-will-not-be-possible/

Answered By: david villa

Answer #6:

The solution was not to install pydot from somewhere, but “python-pydot” from official ubuntu repositories.

Answered By: Jing Zhang

Answer #7:

There are now at least 2 more versions that appear to support PyParsing-2 and Python-3:

Answered By: Sadik

Answer #8:

I had the problem again and my above solution did not work. If that is true for you and you are also using Anaconda on a Mac with El Capitan, try this:

conda install --channel https://conda.anaconda.org/RMG graphviz`
conda install --channel https://conda.anaconda.org/RMG pydot
Answered By: Mark Mikofski

Leave a Reply

Your email address will not be published. Required fields are marked *