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

守給你的承諾、 提交于 2019-12-27 16:23:45

问题


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


回答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.




回答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




回答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:



回答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

  • [1] https://pypi.python.org/pypi/pydot2/1.0.32
  • [2] https://pypi.python.org/pypi/pydot2



回答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/




回答6:


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




回答7:


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

  • PyDotPlus by Carlos Jenkins with a nice travis buildbot and fancy documentation. However you will need to change its folder name from site-packages\pydotplus to site-packages\pydot for it to work with existing programs that import pydot.
  • pydot3k by bmcorser. Sadly, did not work!
  • prologic/pydot by James Mills linked to from the official pydot Google code page as ...

    a branch for Python 3 compatibility

  • and here is a working link to David Villa's pydot2 which works fine: https://pypi.python.org/pypi/pydot2/1.0.32



回答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



回答9:


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'
#



回答10:


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.




回答11:


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



回答12:


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.




回答13:


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.




回答14:


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__


来源:https://stackoverflow.com/questions/15951748/pydot-and-graphviz-error-couldnt-import-dot-parser-loading-of-dot-files-will

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!