Failed to import pydot 和graphviz error: (2, 'RegOpenKeyEx',错误总结

在按照Theano Tutorial编写查看Theano图结构时遇到了点问题。示例代码:

import theano
import theano.tensor as T
from theano import function
import pydot
a = theano.tensor.vector("a")      # declare symbolic variable
b = a + a ** 10                    # build symbolic expression
f = theano.function([a], b)        # compile function
print f([0, 1, 2])                 # prints `array([0,2,1026])`
[    0.,     2.,  1026.]
theano.printing.pydotprint(b, outfile="./pics/symbolic_graph_unopt.png", var_with_name_simple=True)  
theano.printing.pydotprint(f, outfile="./pics/symbolic_graph_opt.png", var_with_name_simple=True)  

error1:

Failed to import pydot

解决方法
用pip安装pydot和graphviz,windows平台下需要下载msi文件手动安装,graphviz下载地址:https://pypi.python.org/pypi/graphviz/0.3.3

重新安装pyparsing:

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

测试一下pydot和graphviz是否安装成功:

import pydot
print pydot.find_graphviz()

如果返回None,说明pydot无法找到graphviz,见error2解决方法。

error2:

graphviz error: (2, 'RegOpenKeyEx'...

pydot无法找到graphviz,手动修改pydot.find_graphviz()函数,例如我是这样改的:

def find_graphviz():
    """Locate Graphviz's executables in the system. Tries three methods: First: Windows Registry (Windows only) This requires Mark Hammond's pywin32 is installed. Secondly: Search the path It will look for 'dot', 'twopi' and 'neato' in all the directories specified in the PATH environment variable. Thirdly: Default install location (Windows only) It will look for 'dot', 'twopi' and 'neato' in the default install location under the "Program Files" directory. It will return a dictionary containing the program names as keys and their paths as values. If this fails, it returns None. """

    # Method 1 (Windows only)
    #
#=======================注释掉这部分内容,不在这个if内搜索graphviz安装目录================
# if os.sys.platform == 'win32':
# 
# try:
# import win32api, win32con
# 
# # Get the GraphViz install path from the registry
# #
# hkey = win32api.RegOpenKeyEx( win32con.HKEY_LOCAL_MACHINE,
# "SOFTWARE\ATT\Graphviz", 0, win32con.KEY_QUERY_VALUE )
# 
# path = win32api.RegQueryValueEx( hkey, "InstallPath" )[0]
# win32api.RegCloseKey( hkey )
# 
# # Now append the "bin" subdirectory:
# #
# path = os.path.join(path, "bin")
# progs = __find_executables(path)
# if progs is not None :
# #print "Used Windows registry"
# return progs
# 
# except ImportError :
# # Print a messaged suggesting they install these?
# #
# pass
# 
#==============================================================================
    # Method 2 (Linux, Windows etc)
    #
    if os.environ.has_key('PATH'):

        for path in os.environ['PATH'].split(os.pathsep):
            progs = __find_executables(path)
            if progs is not None :
                #print "Used path"
                return progs

    # 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 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:\deeplearning\graphviz\bin"

        progs = __find_executables(path)

        if progs is not None :

            #print "Used default install location"
            return progs

    #添加这部分代码,path直接指向graphviz安装路径
    path = r"D:\xxx\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

参考资料:

GraphViz’s executables not found on Windows 7 64-bit if user installs them in a custom directory #91:https://github.com/erocarrera/pydot/issues/91

pydot and graphviz error: Couldn’t import dot_parser, loading of dot files will not be possible:http://stackoverflow.com/questions/15951748/pydot-and-graphviz-error-couldnt-import-dot-parser-loading-of-dot-files-will

RuntimeError: Failed to import pydot. #1801:https://github.com/Theano/Theano/issues/1801

你可能感兴趣的:(Graphviz,theano,pydot,dot-parser)