admin管理员组

文章数量:1406308

I have followed the recommended steps:

  • Installed graphviz on mac using brew install graphviz. Please see below.
ls /usr/local/Cellar/graphviz/12.2.1/bin/
acyclic     diffimg     dot_sandbox graphml2gv  gvmap       gxl2gv      patchwork   twopi
bcomps      dijkstra    edgepaint   gv2gml      gvmap.sh    mm2gv       prune       unflatten
ccomps      dot     fdp     gv2gxl      gvpack      neato       sccmap
circo       dot2gxl     gc      gvcolor     gvpr        nop     sfdp
cluster     dot_builtins    gml2gv      gvgen       gxl2dot     osage       tred
  • Installed the python package using pip3 install graphviz. Please see below.
ls /Users/foo/Library/Python/3.7/lib/python/site-packages/graphviz/
__init__.py     _tools.py       dot.py          jupyter_integration.py  rendering.py
__pycache__     backend         encoding.py     parameters      saving.py
_compat.py      base.py         exceptions.py       piping.py       sources.py
_defaults.py        copying.py      graphs.py       quoting.py      unflattening.py
  • The PATH variable is also set correctly.
echo $PATH
/usr/local/Cellar/graphviz/12.2.1/bin:/usr/local/opt/curl/bin:/usr/local/opt/curl/bin:

However, when I run the python code to generate a basic graph:

import json
import graphviz

if __name__ == '__main__':
    json_string = '{"192.168.133.166":[{}],"192.168.133.180":[{}],"192.168.133.64":[{}]} '

    g1 = graphviz.Digraph(name="my_graph")
    data = json.loads(json_string)

    if isinstance(data, dict):
        for k, v in data.items():
            g1.node(k, None)
            print(k, v)

        # Render the graph
        g1.render(filename="my_graph")

It fails with the following error.

graphviz.backend.execute.ExecutableNotFound: failed to execute PosixPath('dot'), make sure the Graphviz executables are on your systems' PATH

When I run the dot command from command line, it works fine. /usr/local/Cellar/graphviz/12.2.1/bin/dot -T jpeg -O sample.dot

Not sure what is still missing.

I have followed the recommended steps:

  • Installed graphviz on mac using brew install graphviz. Please see below.
ls /usr/local/Cellar/graphviz/12.2.1/bin/
acyclic     diffimg     dot_sandbox graphml2gv  gvmap       gxl2gv      patchwork   twopi
bcomps      dijkstra    edgepaint   gv2gml      gvmap.sh    mm2gv       prune       unflatten
ccomps      dot     fdp     gv2gxl      gvpack      neato       sccmap
circo       dot2gxl     gc      gvcolor     gvpr        nop     sfdp
cluster     dot_builtins    gml2gv      gvgen       gxl2dot     osage       tred
  • Installed the python package using pip3 install graphviz. Please see below.
ls /Users/foo/Library/Python/3.7/lib/python/site-packages/graphviz/
__init__.py     _tools.py       dot.py          jupyter_integration.py  rendering.py
__pycache__     backend         encoding.py     parameters      saving.py
_compat.py      base.py         exceptions.py       piping.py       sources.py
_defaults.py        copying.py      graphs.py       quoting.py      unflattening.py
  • The PATH variable is also set correctly.
echo $PATH
/usr/local/Cellar/graphviz/12.2.1/bin:/usr/local/opt/curl/bin:/usr/local/opt/curl/bin:

However, when I run the python code to generate a basic graph:

import json
import graphviz

if __name__ == '__main__':
    json_string = '{"192.168.133.166":[{}],"192.168.133.180":[{}],"192.168.133.64":[{}]} '

    g1 = graphviz.Digraph(name="my_graph")
    data = json.loads(json_string)

    if isinstance(data, dict):
        for k, v in data.items():
            g1.node(k, None)
            print(k, v)

        # Render the graph
        g1.render(filename="my_graph")

It fails with the following error.

graphviz.backend.execute.ExecutableNotFound: failed to execute PosixPath('dot'), make sure the Graphviz executables are on your systems' PATH

When I run the dot command from command line, it works fine. /usr/local/Cellar/graphviz/12.2.1/bin/dot -T jpeg -O sample.dot

Not sure what is still missing.

Share Improve this question edited Mar 5 at 23:36 AnilJ asked Mar 5 at 21:30 AnilJAnilJ 2,1313 gold badges38 silver badges62 bronze badges 9
  • What happens when you try: dot -V – sroush Commented Mar 5 at 22:36
  • dot -V dot - graphviz version 12.2.1 (20241206.2353) – AnilJ Commented Mar 5 at 22:57
  • What Python code? PosixPath('dot') is basically equivalent to ./dot; it's not going to use your search path to find the dot executable. – chepner Commented Mar 5 at 22:57
  • which dot /usr/local/bin/dot – AnilJ Commented Mar 5 at 23:20
  • 1 maybe script or module runs it with some restrictions - and maybe it runs with empty PATH. You may check os.environ['PATH'] in your code, – furas Commented Mar 6 at 7:04
 |  Show 4 more comments

1 Answer 1

Reset to default 0

For some reason, the python script did not pick the system path env variable. I solved this issue by explicitly setting the path where the 'dot' command is available.

import os 

graphviz_path = "/usr/local/bin/"
os.environ["PATH"] = graphviz_path + os.pathsep + os.environ["PATH"]

print("path: ", os.environ["PATH"])

本文标签: pythonNot able to generate graphviz when runing on MacStack Overflow