Pydot 与 Graphviz 的安装

在学习吴恩达老师的“深度学习”系列课程时,编程作业中要输出 Keras 神经网络模型的模型图,需要安装 pydot 包和 Graphviz 软件,在安装过程中遇到了许多的问题,记录一下。

(平台和环境:win10,Anaconda,pycharm)

具体安装过程分为三个步骤(据说顺序很重要):


step1. 安装 graphviz 包

> pip install graphviz

我使用了 Anaconda 配置的 PyCharm,所以直接在 PyCharm 中下载了 (File -> Default Settings -> Project Interpreter)。

step2. 安装 Graphviz 软件

直接到官网下载,我下载的 msi 格式的安装包,安装成功后把该目录下的 bin 文件夹添加到系统环境变量中。 在 cmd 中输入命令"dot -version"并按回车,若显示出 Graphviz 软件的版本信息,则安装成功。

step3. 安装 pydot 包

如下为安装过程中遇到的坑,解决办法在最后。

刚开始,我打算从 PyCharm 中直接下载,结果报错提示 Pip 版本太低, 就将 Pip 升级到了18.0版本,再次下载 pydot 包,再次报错

>module 'pip' has no attribute 'main'

在网上搜到了解决方案,搬运过来。在安装目录下 PyCharm/helpers/packaging_tool.py文件,找到如下代码:

def do_install(pkgs):
    try:
        import pip
    except ImportError:
        error_no_pip()
    return pip.main(['install'] + pkgs)
        
def do_uninstall(pkgs):
    try:
        import pip
    except ImportError:
        error_no_pip()
    return pip.main(['uninstall', '-y'] + pkgs)

修改为如下代码,保存即可:

def do_install(pkgs):
    try:
        # import pip
        try:
            from pip._internal import main
        except Exception:
            from pip import main
    except ImportError:
        error_no_pip()
    return main(['install'] + pkgs)

def do_uninstall(pkgs):
    try:
        # import pip
        try:
            from pip._internal import main
        except Exception:
            from pip import main
    except ImportError:
        error_no_pip()
    return main(['uninstall', '-y'] + pkgs)

然后又一次下载 pydot 包,运行程序后,报出三条错误,大意是 pydot failed to call graphviz。 只好再次求助网络,一种解决办法说:现在 pydot 默认安装的是1.2.4版本,此版本已经不支持 find_graphviz() 函数,需要安装1.1.0版本。很无奈,只好去试试了。

>pip install pydot==1.1.0

在 cmd 输入上述命令,又报错,装不了。一种解决办法是手动安装,但是下载安装包要论坛币,伤心=_= 。 安装1.1.0版本的第二种办法是先安装 pyparsing 1.5.7版本。

>pip install pyparsing==1.5.7
>pip install pydot==1.1.0

又又又又报错了,降低版本怎么就这么难啊!

解决办法

在经历了这么多的弯弯绕绕后,最终在 stackoverflow 网站的一个问题下找到一种我的电脑可以用的方法,感谢大神啊!

首先下载 pydotplus 包。

>pip install pydotplus

然后找到 pydotplus 包的存储位置,将包名更改为pydot (会有两个文件包,pydotplus 和 pydotplus-2.0.2.dist-info , 只要修改前一个包名就可以了)。例如我的存储位置在D:\Anaconda3\Lib\site-packages\pydotplus , 修改为D:\Anaconda3\Lib\site-packages\pydot。

再打开 pydot 文件夹中的 parser.py文件,将

import pydotplus 

修改为

import pydot

运行程序,再次报错。

pydot.InvocationException: GraphViz's executables not found

打开 pydot 文件夹中的 graphviz.py文件,找到 find_graphviz() 函数。将

#Method 3 (Windows only)

这行之后的代码替换为下列代码。

# Method 3 (Windows only)
#
if os.sys.platform == 'win32':
    # Try and work out the equivalent of "C:\Program Files" on this
    # machine (might be on drive D:, or in a different language)
    #
    if False:  # os.environ.has_key('PROGRAMFILES'):
        # Note, we could also use the win32api to get this
        # information, but win32api may not be installed.
        path = os.path.join(os.environ['PROGRAMFILES'], 'ATT', 'GraphViz', 'bin')
    else:
        # Just in case, try the default...
        path = r"D:\Graphviz\bin"
    progs = __find_executables(path)

    if progs is not None:
        # print "Used default install location"
        return progs

for path in (
        '/usr/bin', '/usr/local/bin',
        '/opt/bin', '/sw/bin', '/usr/share',
        '/Applications/Graphviz.app/Contents/MacOS/'):
    progs = __find_executables(path)

    if progs is not None:
        # print "Used path"
        return progs
# Failed to find GraphViz
#

return None

其中 D:\Graphviz\bin 是 Graphviz 软件在我电脑上的安装路径,将其更改为你的安装路径。

到这里应该安装成功了,运行程序就能输出模型图了。前前后后花了将近一个白天的时间,时不我待啊!

引用:

[1]解决pycharm问题:module 'pip' has no attribute 'main'

[2]python的数据可视化 graphviz pydot安装配置(win10)

[3]《安装pydot(和Graphviz)》

[4]python – pydot和graphviz错误:无法导入dot_parser,无法加载点文件

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

你可能感兴趣的:(deep,learning)