使用python pydot时出错

1 投票
2 回答
6209 浏览
提问于 2025-04-18 18:02

我在使用theano.printing.pydotprint()这个函数时遇到了错误。

以下这些代码运行得很好,没有任何错误:

>>> from theano import *
>>> import theano.tensor as T
>>> import pydot
>>> import numpy

而当我在Python解释器中运行

>>> theano.printing.pydotprint

时,得到的输出是

<function pydotprint at 0x307ed70>

但问题是,当我用这个函数执行脚本时,我却遇到了以下错误:

RuntimeError: Failed to import pydot. You must install pydot for `pydotprint` to work.

有没有人知道这是什么问题呢?

附注:我正在按照这里的Python教程进行操作:http://deeplearning.net/software/theano/tutorial/printing_drawing.html,所以调用这个函数肯定是正确的。

这是我遇到的错误追踪信息:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/usr/lib/python2.7/dist-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
    173             else:
    174                 filename = fname
--> 175             __builtin__.execfile(filename, *where)

/home/abhishek/Desktop/Theano/test2.py in <module>()
     64 if not os.path.exists('pics'):
     65     os.mkdir('pics')
---> 66 theano.printing.pydotprint(predict,outfile="pics/logreg_pydotprint_predic.png",var_with_name_simple=True)
     67 # before compilation

     68 theano.printing.pydotprint_variables(prediction,outfile="pics/logreg_pydotprint_prediction.png",var_with_name_simple=True)

/usr/local/lib/python2.7/dist-packages/theano/printing.pyc in pydotprint(fct, outfile, compact, format, with_ids, high_contrast, cond_highlight, colorCodes, max_label_size, scan_graphs, var_with_name_simple, print_output_file, assert_nb_all_strings)
    566 
    567     if not pydot_imported:
--> 568         raise RuntimeError("Failed to import pydot. You must install pydot"
    569                             " for `pydotprint` to work.")
    570         return

RuntimeError: Failed to import pydot. You must install pydot for `pydotprint` to work.

2 个回答

1

Abhishek:检查一下你是否能在lib文件夹下看到“pydot”文件夹。看起来你是在ix系统上。通常情况下,你可以在'lib'文件夹或者site-packages文件夹里找到它是否已经安装。

同时,我建议你尝试重新安装一下这个包(pydot),看看这样是否能解决问题。

4

我尝试按照这个解决方案重新安装pydot,但还是不行。

也就是说

pip uninstall pyparsing
pip install -Iv https://pypi.python.org/packages/source/p/pyparsing/pyparsing-1.5.7.tar.gz#md5=9be0fcdcc595199c646ab317c1d9a709
pip install pydot

这个安装过程中出现了一些问题,尽管每次都显示安装成功。

但是

sudo apt-get install python-pydot

这个方法有效。

“因为解决方案不是从其他地方安装pydot,而是从官方的ubuntu软件库安装‘python-pydot’。” - sadik的回答有效。

我们需要注意的是,成功安装pydot后,可以在两个地方检查它是否安装成功。
/usr/share/doc/python-pydot

/usr/share/python-support/python-pydot

enter image description here

撰写回答