106

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

Sadık
  • 4,249
  • 7
  • 53
  • 89
  • 6
    `pip install pydot2` should fix this issue. Much nicer than using `pip uninstall` because if you share code you can just add pydot2 to your requirements.txt and not have users run unfamiliar commands. – RussellStewart Jul 25 '15 at 20:45

14 Answers14

219

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.

Community
  • 1
  • 1
Jonathan
  • 5,736
  • 2
  • 24
  • 22
  • 1
    I installed NetworkX, GraphViz and pydot, but could not get Graphviz functionality to work with NetworkX (e.g. [this](http://networkx.github.io/documentation/latest/examples/drawing/circular_tree.html) NetworkX example failed). I kept getting: **global name 'dot_parser' is not defined**. Your solution solved this problem. – qtips Aug 06 '13 at 00:41
  • 1
    also, `sudo pip uninstall` does not work if you have installed your package using `python setup.py install` in that case, follow [this](http://stackoverflow.com/questions/1550226/python-setup-py-uninstall) solution. This was at least the case on my Mountain Lion OSX Mac – qtips Aug 06 '13 at 00:43
  • `sudo` is obviously not always necessary, like if you're using a virtualenv. Other than that, this worked for me! – David Reynolds Nov 14 '13 at 10:43
  • In Ubuntu 12.04 I get this error, when installing byparsing: "Requested pyparsing==1.5.7, but installing version 2.0.1". Then the error above occurs still So the solution was to specify the download location of the python egg: sudo pip install -Iv https://pypi.python.org/packages/source/p/pyparsing/pyparsing-1.5.7.tar.gz#md5=9be0fcdcc595199c646ab317c1d9a709 – lefterav Jan 20 '14 at 10:08
  • Minor correction for folks who uninstalled pydot before re-installing pyparsing and then re-installing pydot) "sudo pip install Graphviz" before "sudo pip install pydot" . Since pydot needs BOTH Graphviz & pyparsing" - post this I did not get the "Couldn't import dot_parser, loading of dot files will not be possible" error – ekta Feb 11 '14 at 08:03
  • This didn't work for me. The solution by Dana The Sane did. In addition it doesn't requires to downgrade your versions. – Picarus Apr 11 '14 at 13:18
  • 45
    Why not just `pip install pyparsing==1.5.7`? – Suor Jun 13 '14 at 07:19
  • This also works for ``NameError: global name 'dot_parser' is not defined`` – Zachary Jacobi Jul 24 '14 at 14:06
  • @Sadik's answer should also be mentioned here. For some reason pip install does not update path correctly on Ubuntu 14.04. `sudo apt-get install python-pydot` – Pramit Jul 21 '16 at 17:50
  • For me on pydot 1.0.29, I needed to install pyparsing==2.0.6, as suggested by [this tangentially-related issue](https://github.com/stevearc/dql/issues/4). :-/ – tsbertalan Oct 01 '16 at 13:14
57

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

Dana the Sane
  • 14,762
  • 8
  • 58
  • 80
  • 7
    IMHO This answer should be considered over the pyparsing downgrading Idea. Works for me perfectly with networkx too. Thanks ! – Raghav RV Jul 05 '14 at 16:38
  • Still giving this error but dot_parser error gone. You must install pydot for `pydotprint` to work. – Inanc Gumus Oct 23 '14 at 10:55
20

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:
Gabi Davar
  • 959
  • 10
  • 13
  • 2
    Just for completeness, [here's the issue report](http://code.google.com/p/pydot/issues/detail?id=81#c9) for pydot which reports this issue and the fix mentioned by @Gabi Davar. – Jack Kelly Oct 16 '13 at 09:11
7

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

0 _
  • 10,524
  • 11
  • 77
  • 109
david villa
  • 384
  • 4
  • 9
  • 1
    The PyPI page appears to be down. That's too bad! Any ideas why? – Dav Clark Nov 12 '14 at 16:32
  • the pip package seems to be broken. `pip install...` failed in the middle without explicit error. Looks like the manifest and the actual files don't match. I checkout the git repo, and `mv README.rst README` then `python setup.py install`. This worked for me :-) – Kenji Noguchi Dec 17 '14 at 19:20
  • 2
    the link, https://pypi.python.org/pypi/pydot2, is broken but `pip install pydot2` works and this link, https://pypi.python.org/pypi/pydot2/1.0.32, works. – Mark Mikofski Feb 17 '15 at 19:45
6

$ 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/

Jing Zhang
  • 69
  • 1
  • 1
  • I did vice versa ```pip uninstall pydot2``` and then ```pip install pydot``` to fix the error. Here is the link on the correct answer: https://github.com/pydot/pydot/issues/166#issuecomment-356066120 – FooBar167 Jan 18 '19 at 15:31
5

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

Sadık
  • 4,249
  • 7
  • 53
  • 89
  • As of 25-Aug-2013 `pip install python-pydot` does not work for me (distribution not found); `pip install pydot` does. So I don't think it's that `pydot` is necessarily unofficial. Perhaps you were installing via an OS distribution instead? – Owen S. Aug 25 '13 at 19:38
  • 5
    Not with pip, `sudo apt-get install python-pydot` did it for me. – Sadık Aug 25 '13 at 20:38
  • That would probably work because Ubuntu currently has `python-pyparsing` at 1.5.2, which avoids the version incompatibility listed above. I went with the fix above and haven't had problems since. Thanks! – Owen S. Aug 25 '13 at 20:41
  • 1
    Unfortunately in Ubuntu as of Saucy pyparsing has been upgraded but pydot has not been patched appropriately. – Tully May 20 '14 at 06:18
  • 1
    I've filed a ticket to fix the Ubuntu package: https://bugs.launchpad.net/ubuntu/+source/pydot/+bug/1321135 – Tully May 20 '14 at 06:45
  • i was getting "GraphViz's executables not found" error and tried various things (try to set path for grarphviz, uninstall / install pyparser, pydot) but on my Ubuntu precise this is the only thing worked for me `sudo apt-get install python-pydot` thank you! – naoko Aug 15 '14 at 02:09
3

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

Mark Mikofski
  • 19,398
  • 2
  • 57
  • 90
3

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
scottlittle
  • 18,866
  • 8
  • 51
  • 70
3

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'
#
sten
  • 7,028
  • 9
  • 41
  • 63
2

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.

scottlittle
  • 18,866
  • 8
  • 51
  • 70
1

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
ecordo
  • 7
  • 1
1

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.

Totoro
  • 3,398
  • 1
  • 24
  • 39
1

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.

Cna
  • 11
  • 1
0

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__
Sander van den Oord
  • 10,986
  • 5
  • 51
  • 96