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

Question:

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

||

Answers:

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

Answered By: Sadik

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: Jonathan

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: Gabi Davar

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: david villa

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: Dana the Sane

On OSX Mavericks the following did the trick… I got the same error but at the bottom there was also a complaint that the graphviz executable was not present… I think the problem was i had installed graphviz prior to the other modules?

brew uninstall graphviz
brew install graphviz
Answered By: ecordo

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

Answered By: Mark Mikofski

$ 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: Jing Zhang

This worked for me (Mac OS X 10.9 with Python 2.7.10 on Anaconda):

conda uninstall pydot

Then,

conda install pydot

Pyparsing is then downgraded (from 2.x to 1.5.7) upon pydot’s installation. Future Googlers: this allowed me to install and import Theano correctly.

Answered By: scottlittle

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: scottlittle

You need to downgrade pyparsing from version 2.x to version 1.5.7 to get pydot to work correctly.

For win-64, using Conda, this worked for me:

conda install -c https://conda.anaconda.org/Trentonoliphant pyparsing=1.5.7

I then disabled/uninstalled the 2.x version and reloaded pyparsing in my script:

pyparsing = reload(pyparsing)
pydot = reload(pydot)

To check whether you have the right version running:

print pyparsing.__version__
Answered By: Sander van den Oord

When other solutions do not work, this is a quick and dirty method to solve the probem:

This example is from python 2.7 on Ubuntu 16.04.

Edit the file python2.7/site-packages/keras/utils/visualize_util.py and comment the code segment below.

if not pydot.find_graphviz():
    raise ImportError('Failed to import pydot. You must install pydot'
                      ' and graphviz for `pydotprint` to work.')

find_graphviz() is redundant on newer versions of pydot, and the above call does not work.

Answered By: Totoro

What I did at the end after so many tries from what i saw here (pseudo sequence for it to work for networkx ) :

apt-get remove python-pydot
pip install pydotplus
apt-get install libcgraph6
apt-get install python-pygraphviz


# pip freeze | grep pydot
 pydotplus==2.0.2
# pip freeze | grep pyparsing
pyparsing==2.2.0
# pip freeze | grep graphviz
pygraphviz==1.2
# python -c 'import pydotplus'
#
Answered By: sten

I also met the problem and my pydot==1.0.28 while pyparsing==2.2.0. I fixed the problem by downloading the newest pydot 1.2.3(tar.gz)from google and then install it offline. When I updated the pydot in ubuntu 14.04, it said the pydot 1.0.28 is the newest version. Therefore I download from the google the 1.2.3 version.

Answered By: Cna
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.