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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15951748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:26:11  来源:igfitidea点击:

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

pythonpython-2.7graphvizpydot

提问by Sadik

When I run a very simple code with pydot

当我用 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

我正在使用 python 2.7.3

采纳答案by Jonathan

Answer for pydot >= 1.1:

回答pydot >= 1.1

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

(upstream) 的不兼容性pydot已由6dff94b3f1 修复,因此pydot >= 1.1pyparsing >= 1.5.7.



Answer applicable to pydot <= 1.0.28:

答案适用于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:

对于遇到此问题的任何其他人,这是由于 pyparsing 从 1.x 到 2.x 版本的更改。要使用 pip 安装 pydot,首先安装旧版本的 pyparsing:

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

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

如果您没有安装pyparsingusing pip,而是 used setup.py,请查看此解决方案以卸载软件包。谢谢@qtips。

回答by Sadik

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

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

回答by Gabi Davar

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

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:

回答by david villa

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].

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

Cheers

干杯

回答by Dana the Sane

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.

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

Note: python2.7 from macports

注意:来自macports的python2.7

回答by ecordo

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?

在 OSX Mavericks 上,以下操作成功了……我遇到了同样的错误,但在底部也有人抱怨 graphviz 可执行文件不存在……我认为问题是我在其他模块之前安装了 graphviz?

brew uninstall graphviz
brew install graphviz

回答by Mark Mikofski

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

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

  • PyDotPlusCarlos Jenkins提供,带有漂亮的 travis buildbot 和精美的文档。但是,您需要将其文件夹名称从site-packages\pydotplus更改site-packages\pydot为 ,才能与导入 pydot 的现有程序一起使用。
  • pydot3kbmcorser可悲的是,没有奏效!
  • 詹姆斯米尔斯的prologic/pydot从官方 pydot 谷歌代码页链接到......

    Python 3 兼容性的分支

  • 这是 David Villa 的 pydot2 的工作链接,它工作正常: https://pypi.python.org/pypi/pydot2/1.0.32

回答by Jing Zhang

$ sudo pip uninstall pydot

$ sudo pip uninstall pydot

$ sudo pip install pydot2

$ sudo pip install pydot2

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

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

回答by scottlittle

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

这对我有用(Mac OS X 10.9 和 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.

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

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

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

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