问题:pydot和graphviz错误:无法导入dot_parser,将无法加载点文件

当我使用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')

它向我显示错误消息:

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

我正在使用python 2.7.3

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


回答 0

回答pydot >= 1.1

(上游)的不兼容pydot已由6dff94b3f1修复,因此pydot >= 1.1将与兼容pyparsing >= 1.5.7


答案适用于pydot <= 1.0.28

对于遇到此问题的其他人,这是由于pyparsing从1.x到2.x版本的更改所致。要使用pip安装pydot,请先安装较早版本的pyparsing:

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

如果您不是pyparsing使用进行安装pip,而是使用进行了安装,请setup.py查看此解决方案以卸载该软件包。谢谢@qtips。

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.


回答 1

pip存储库中有一个名为pydot2的新程序包,可与pyparsing2一起正常运行。我无法降级我的软件包,因为matplotlib依赖于较新的pyparsing软件包。

注意:来自Macports的python2.7

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


回答 2

pydot使用了来自pyparsing的私有模块变量(_noncomma)。下面的差异修复了它用于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:

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:

回答 3

我分叉了pydot存储库[1],应用了Gabi Davar补丁和一些更改来支持python-3。该软件包可在PyPI [2]中找到。

干杯

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


回答 4

$ sudo pip uninstall pydot

$ sudo pip install pydot2

请参阅以下链接:http : //infidea.net/troubleshooting-couldnt-import-dot_parser-loading-of-dot-files-will-not-be-possible/


回答 5

解决方案不是从某个地方安装pydot,而是从官方ubuntu存储库安装“ python-pydot”。

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


回答 6

现在至少有2个版本似乎支持PyParsing-2和Python-3:

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


回答 7

我又遇到了问题,上述解决方案不起作用。如果这对您来说是正确的,并且您还正在Mac Cap和El Capitan上使用Anaconda,请尝试以下操作:

conda install --channel https://conda.anaconda.org/RMG graphviz`
conda install --channel https://conda.anaconda.org/RMG pydot

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

回答 8

经过多次尝试后,最后我做了什么(伪序列使其适用于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'
#

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

回答 9

这对我有用(Anaconda上具有Python 2.7.10的Mac OS X 10.9):

conda uninstall pydot

然后,

conda install pydot

然后在安装pydot之后将Pyparsing降级(从2.x降级到1.5.7)。未来的Google员工:这使我能够正确安装和导入Theano。

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.


回答 10

在OSX Mavericks上,以下技巧可以解决问题:我遇到了相同的错误,但是在底部,还抱怨没有graphviz可执行文件…我认为问题是我在其他模块之前安装了graphviz吗?

brew uninstall graphviz
brew install graphviz

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

回答 11

当其他解决方案不起作用时,这是解决探针的一种快速而肮脏的方法:

这个例子来自Ubuntu 16.04上的python 2.7。

编辑文件python2.7 / site-packages / keras / utils / visualize_util.py并注释以下代码段。

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

find_graphviz()在较新版本的pydot上是多余的,并且上述调用不起作用。

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.


回答 12

我也遇到了这个问题,而我的pydot == 1.0.28而pyparsing == 2.2.0。我通过从Google下载最新的pydot 1.2.3(tar.gz)解决了问题,然后离线安装了它。当我在ubuntu 14.04中更新pydot时,它说pydot 1.0.28是最新版本。因此,我从谷歌下载1.2.3版本。

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.


回答 13

您需要将pyparsing从2.x版本降级到1.5.7,才能使pydot正常工作。

对于使用Conda的Win-64,这对我有用:

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

然后,我禁用/卸载了2.x版本,并在脚本中重新加载了pyparsing:

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

要检查您是否运行了正确的版本:

print pyparsing.__version__

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__

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。