Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to pass explicitly path to GraphViz executable (useful in Windows 7) #91

Closed
GoogleCodeExporter opened this issue Apr 13, 2015 · 8 comments
Assignees
Milestone

Comments

@GoogleCodeExporter
Copy link

GoogleCodeExporter commented Apr 13, 2015

I am running Windows 7 64-bit. I installed graphviz-2.36.msi from
http://www.graphviz.org/Download_windows.php but I install most programs to
c:\app rather than c:\Program Files, and I get this message (see below). One
problem is that there is no SOFTWARE\ATT registry key anymore. (perhaps related
to https://code.google.com/p/pydot/issues/detail?id=65)

I think the bigger problem is that this discovery mechanism is a cat-and-mouse
game with whatever the graphviz folks do.

===> Could you please add a function so that end-users can just set the
graphviz path manually through whatever mechanism they see fit?

For now I'm just monkeypatching the pydot module:

def force_find_graphviz(graphviz_root):
   binpath = os.path.join(graphviz_root, 'bin')
   programs = 'dot twopi neato circo fdp sfdp'
   def helper():
     for prg in programs.split():
       if os.path.exists(os.path.join(binpath, prg)):
         yield ((prg, os.path.join(binpath, prg)))
       elif os.path.exists(os.path.join(binpath, prg+'.exe')):
         yield ((prg, os.path.join(binpath, prg+'.exe')))
   progdict = dict(helper())
   return lambda: progdict

pydot.find_graphviz = force_find_graphviz('c:/app/util/graphviz/2.36')



C:\tmp\blockdiag>python
Python 2.7.5 |Anaconda 1.9.1 (64-bit)| (default, May 31 2013, 10:45:37) [MSC v.1
500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pydot
Couldn't import dot_parser, loading of dot files will not be possible.
>>> graph = pydot.Dot(graph_type='graph')
>>> graph.write_png('test.png')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\app\python\anaconda\1.6.0\lib\site-packages\pydot.py", line 1602, in
<lambda>
    lambda path, f=frmt, prog=self.prog : self.write(path, format=f, prog=prog))

  File "c:\app\python\anaconda\1.6.0\lib\site-packages\pydot.py", line 1696, in
write
    dot_fd.write(self.create(prog, format))
  File "c:\app\python\anaconda\1.6.0\lib\site-packages\pydot.py", line 1724, in
create
    self.progs = find_graphviz()
  File "c:\app\python\anaconda\1.6.0\lib\site-packages\pydot.py", line 409, in f
ind_graphviz
    "SOFTWARE\ATT\Graphviz", 0, win32con.KEY_QUERY_VALUE )
pywintypes.error: (2, 'RegOpenKeyEx', 'The system cannot find the file specified
.')
>>>

Original issue reported on code.google.com by jmsa...@gmail.com on 28 Mar 2014 at 7:30

@GoogleCodeExporter
Copy link
Author

GoogleCodeExporter commented Apr 13, 2015

Hello,

I have windows 7 32 bit and python 2.7

Here how I installed pydot for python

first I installed GraphViz using this link:
http://www.graphviz.org/content/how-install-graphviz-windows-7-i-cant-find-set-file
after that I installed Pyparsing then pydot (from this answer
http://stackoverflow.com/questions/15951748/pydot-and-graphviz-error-couldnt-import-dot-parser-loading-of-dot-files-will)

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

After that when I tried this line of code:

graph.write_png('test.png')

I had a similar error:

File "c:\app\python\anaconda\1.6.0\lib\site-packages\pydot.py", line 409, in find_graphviz
    "SOFTWARE\ATT\Graphviz", 0, win32con.KEY_QUERY_VALUE )
pywintypes.error: (2, 'RegOpenKeyEx', 'The system cannot find the file specified.')

I solved this problem by changing the function find_graphviz() in the pydot.py file, here is the code that I used:

def find_graphviz():
    if os.sys.platform == 'win32':


        if os.environ.has_key('PROGRAMFILES'):
            #Change to your path
            path =  "C:\Program Files\Graphviz2.30\\bin"


        else:

            #Just in case, try the default...
            path = r"C:\Program Files\Graphviz2.30\bin"

        progs = __find_executables(path)

        if progs is not None :

            #print "Used default install location"

            return progs


       return None

Hope it helps.
Cheers

Original comment by sara.ela...@gmail.com on 9 Feb 2015 at 5:29

@KentChun33333
Copy link

This saves my day! Thanks : )

@RunshengSong
Copy link

Saved my day too!!

@Ben-Gillman
Copy link

Ben-Gillman commented Jan 15, 2016

I'm running Windows 10 with Python 2.7 on the Anaconda Prompt.

I have replaced the def find_graphviz function with your code above.

But when I run Import Pydot, I receive the following error:

      File "C:\Users\...\AppData\Local\Continuum\Anaconda2\lib\site-packages\pydot.py", line 432
        return None
                   ^
      IndentationError: unindent does not match any outer indentation level

This is how I have the function currently defined in pydot.py:

    def find_graphviz():
        if os.sys.platform == 'win32':


        if os.environ.has_key('PROGRAMFILES'):
            #Change to your path
            path =  "C:\Program Files\Graphviz2.30\\bin"


        else:

            #Just in case, try the default...
            path = r"C:\Program Files\Graphviz2.30\bin"

        progs = __find_executables(path)

        if progs is not None :

            #print "Used default install location"

            return progs


        return None

Please let me know how to alleviate this error.

Thank you,
Ben

@johnyf
Copy link
Contributor

johnyf commented May 14, 2016

The OP suggests that the user be able to pass the path of GraphViz. The standard approach to this problem is to add GraphViz to the path. Hardcoding its path is not a solution #109. Nonetheless, adding an argument graphviz_path to write and its write_* siblings is flexible, and can accommodate also selecting a GraphViz executable from within Python code, in any OS, without modifying the $PATH.

Thanks for the suggestion, this should also provide an alternative for #109, #102, #65 for users that prefer to not change the path.

@johnyf johnyf changed the title GraphViz's executables not found on Windows 7 64-bit if user installs them in a custom directory add option to pass explicitly path to GraphViz executable (useful in Windows 7) May 14, 2016
@johnyf johnyf self-assigned this May 14, 2016
@johnyf
Copy link
Contributor

johnyf commented Jun 30, 2016

Addressed by f3abbd5, which allows passing the path to the desired GraphViz executable as the prog argument of method pydot.Dot.write_* (for example, write_pdf('filename.pdf', prog='C:\\some\\path\\dot.exe'). It remains to test this on Windows.

The mentioned change also implements properly $PATH searching, by using os.environ['PATH'] and the env argument of subprocess.

Relevant discussion and issues:
http://stackoverflow.com/a/5659249/1959808
http://bugs.python.org/issue8557

@jecs89
Copy link

jecs89 commented Nov 27, 2017

I found a manual solution:
sudo apt-get install graphviz

graph.write('test.dot')
dot -Tps test.dot -o outfile.ps

You can the files here: https://github.com/jecs89/LearningEveryDay/tree/master/GP

@johnyf
Copy link
Contributor

johnyf commented Nov 27, 2017

Please see #126 (comment)

@pydot pydot locked and limited conversation to collaborators Nov 27, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants