17

I'm having trouble installing pygraphviz and I'm using Anaconda on macOS Monterey.

I already had graphviz on Anaconda. I then did

brew install graphviz

and then

pip install pygraphviz

but I keep getting this error:

    Collecting pygraphviz
  Using cached pygraphviz-1.7.zip (118 kB)
Building wheels for collected packages: pygraphviz
  Building wheel for pygraphviz (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: /Users/susiekim4/opt/anaconda3/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/j5/z34rlnbj2l94p1hh0x_hkkjw0000gn/T/pip-install-zr2twxnl/pygraphviz_e78bc2c823034812a5ffbd6ef61160d7/setup.py'"'"'; __file__='"'"'/private/var/folders/j5/z34rlnbj2l94p1hh0x_hkkjw0000gn/T/pip-install-zr2twxnl/pygraphviz_e78bc2c823034812a5ffbd6ef61160d7/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /private/var/folders/j5/z34rlnbj2l94p1hh0x_hkkjw0000gn/T/pip-wheel-4fuwg_2b
       cwd: /private/var/folders/j5/z34rlnbj2l94p1hh0x_hkkjw0000gn/T/pip-install-zr2twxnl/pygraphviz_e78bc2c823034812a5ffbd6ef61160d7/
  Complete output (71 lines):
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.macosx-10.9-x86_64-3.8
  creating build/lib.macosx-10.9-x86_64-3.8/pygraphviz
  copying pygraphviz/scraper.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz
  copying pygraphviz/graphviz.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz
  copying pygraphviz/__init__.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz
  copying pygraphviz/agraph.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz
  copying pygraphviz/testing.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz
  creating build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
  copying pygraphviz/tests/test_unicode.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
  copying pygraphviz/tests/test_scraper.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
  copying pygraphviz/tests/test_readwrite.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
  copying pygraphviz/tests/test_string.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
  copying pygraphviz/tests/__init__.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
  copying pygraphviz/tests/test_html.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
  copying pygraphviz/tests/test_node_attributes.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
  copying pygraphviz/tests/test_drawing.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
  copying pygraphviz/tests/test_subgraph.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
  copying pygraphviz/tests/test_close.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
  copying pygraphviz/tests/test_edge_attributes.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
  copying pygraphviz/tests/test_clear.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
  copying pygraphviz/tests/test_layout.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
  copying pygraphviz/tests/test_attribute_defaults.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
  copying pygraphviz/tests/test_graph.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
  running egg_info
  writing pygraphviz.egg-info/PKG-INFO
  writing dependency_links to pygraphviz.egg-info/dependency_links.txt
  writing top-level names to pygraphviz.egg-info/top_level.txt
  reading manifest file 'pygraphviz.egg-info/SOURCES.txt'
  reading manifest template 'MANIFEST.in'
  warning: no files found matching '*.png' under directory 'doc'
  warning: no files found matching '*.txt' under directory 'doc'
  warning: no files found matching '*.css' under directory 'doc'
  warning: no previously-included files matching '*~' found anywhere in distribution
  warning: no previously-included files matching '*.pyc' found anywhere in distribution
  warning: no previously-included files matching '.svn' found anywhere in distribution
  no previously-included directories found matching 'doc/build'
  writing manifest file 'pygraphviz.egg-info/SOURCES.txt'
  copying pygraphviz/graphviz.i -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz
  copying pygraphviz/graphviz_wrap.c -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz
  running build_ext
  building 'pygraphviz._graphviz' extension
  creating build/temp.macosx-10.9-x86_64-3.8
  creating build/temp.macosx-10.9-x86_64-3.8/pygraphviz
  gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/susiekim4/opt/anaconda3/include -arch x86_64 -I/Users/susiekim4/opt/anaconda3/include -arch x86_64 -I/Users/susiekim4/opt/anaconda3/include/python3.8 -c pygraphviz/graphviz_wrap.c -o build/temp.macosx-10.9-x86_64-3.8/pygraphviz/graphviz_wrap.o
  pygraphviz/graphviz_wrap.c:1756:7: warning: 'tp_print' is deprecated [-Wdeprecated-declarations]
        0,                                    /* tp_print */
        ^
  /Users/susiekim4/opt/anaconda3/include/python3.8/cpython/object.h:260:5: note: 'tp_print' has been explicitly marked deprecated here
      Py_DEPRECATED(3.8) int (*tp_print)(PyObject *, FILE *, int);
      ^
  /Users/susiekim4/opt/anaconda3/include/python3.8/pyport.h:515:54: note: expanded from macro 'Py_DEPRECATED'
  #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                       ^
  pygraphviz/graphviz_wrap.c:1923:7: warning: 'tp_print' is deprecated [-Wdeprecated-declarations]
        0,                                    /* tp_print */
        ^
  /Users/susiekim4/opt/anaconda3/include/python3.8/cpython/object.h:260:5: note: 'tp_print' has been explicitly marked deprecated here
      Py_DEPRECATED(3.8) int (*tp_print)(PyObject *, FILE *, int);
      ^
  /Users/susiekim4/opt/anaconda3/include/python3.8/pyport.h:515:54: note: expanded from macro 'Py_DEPRECATED'
  #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                       ^
  pygraphviz/graphviz_wrap.c:2711:10: fatal error: 'graphviz/cgraph.h' file not found
  #include "graphviz/cgraph.h"
           ^~~~~~~~~~~~~~~~~~~
  2 warnings and 1 error generated.
  error: command 'gcc' failed with exit status 1
  ----------------------------------------
  ERROR: Failed building wheel for pygraphviz
  Running setup.py clean for pygraphviz
Failed to build pygraphviz
Installing collected packages: pygraphviz
    Running setup.py install for pygraphviz ... error
    ERROR: Command errored out with exit status 1:
     command: /Users/susiekim4/opt/anaconda3/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/j5/z34rlnbj2l94p1hh0x_hkkjw0000gn/T/pip-install-zr2twxnl/pygraphviz_e78bc2c823034812a5ffbd6ef61160d7/setup.py'"'"'; __file__='"'"'/private/var/folders/j5/z34rlnbj2l94p1hh0x_hkkjw0000gn/T/pip-install-zr2twxnl/pygraphviz_e78bc2c823034812a5ffbd6ef61160d7/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/j5/z34rlnbj2l94p1hh0x_hkkjw0000gn/T/pip-record-pnja99aw/install-record.txt --single-version-externally-managed --compile --install-headers /Users/susiekim4/opt/anaconda3/include/python3.8/pygraphviz
         cwd: /private/var/folders/j5/z34rlnbj2l94p1hh0x_hkkjw0000gn/T/pip-install-zr2twxnl/pygraphviz_e78bc2c823034812a5ffbd6ef61160d7/
    Complete output (71 lines):
    running install
    running build
    running build_py
    creating build
    creating build/lib.macosx-10.9-x86_64-3.8
    creating build/lib.macosx-10.9-x86_64-3.8/pygraphviz
    copying pygraphviz/scraper.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz
    copying pygraphviz/graphviz.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz
    copying pygraphviz/__init__.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz
    copying pygraphviz/agraph.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz
    copying pygraphviz/testing.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz
    creating build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
    copying pygraphviz/tests/test_unicode.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
    copying pygraphviz/tests/test_scraper.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
    copying pygraphviz/tests/test_readwrite.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
    copying pygraphviz/tests/test_string.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
    copying pygraphviz/tests/__init__.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
    copying pygraphviz/tests/test_html.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
    copying pygraphviz/tests/test_node_attributes.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
    copying pygraphviz/tests/test_drawing.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
    copying pygraphviz/tests/test_subgraph.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
    copying pygraphviz/tests/test_close.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
    copying pygraphviz/tests/test_edge_attributes.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
    copying pygraphviz/tests/test_clear.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
    copying pygraphviz/tests/test_layout.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
    copying pygraphviz/tests/test_attribute_defaults.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
    copying pygraphviz/tests/test_graph.py -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz/tests
    running egg_info
    writing pygraphviz.egg-info/PKG-INFO
    writing dependency_links to pygraphviz.egg-info/dependency_links.txt
    writing top-level names to pygraphviz.egg-info/top_level.txt
    reading manifest file 'pygraphviz.egg-info/SOURCES.txt'
    reading manifest template 'MANIFEST.in'
    warning: no files found matching '*.png' under directory 'doc'
    warning: no files found matching '*.txt' under directory 'doc'
    warning: no files found matching '*.css' under directory 'doc'
    warning: no previously-included files matching '*~' found anywhere in distribution
    warning: no previously-included files matching '*.pyc' found anywhere in distribution
    warning: no previously-included files matching '.svn' found anywhere in distribution
    no previously-included directories found matching 'doc/build'
    writing manifest file 'pygraphviz.egg-info/SOURCES.txt'
    copying pygraphviz/graphviz.i -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz
    copying pygraphviz/graphviz_wrap.c -> build/lib.macosx-10.9-x86_64-3.8/pygraphviz
    running build_ext
    building 'pygraphviz._graphviz' extension
    creating build/temp.macosx-10.9-x86_64-3.8
    creating build/temp.macosx-10.9-x86_64-3.8/pygraphviz
    gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/susiekim4/opt/anaconda3/include -arch x86_64 -I/Users/susiekim4/opt/anaconda3/include -arch x86_64 -I/Users/susiekim4/opt/anaconda3/include/python3.8 -c pygraphviz/graphviz_wrap.c -o build/temp.macosx-10.9-x86_64-3.8/pygraphviz/graphviz_wrap.o
    pygraphviz/graphviz_wrap.c:1756:7: warning: 'tp_print' is deprecated [-Wdeprecated-declarations]
          0,                                    /* tp_print */
          ^
    /Users/susiekim4/opt/anaconda3/include/python3.8/cpython/object.h:260:5: note: 'tp_print' has been explicitly marked deprecated here
        Py_DEPRECATED(3.8) int (*tp_print)(PyObject *, FILE *, int);
        ^
    /Users/susiekim4/opt/anaconda3/include/python3.8/pyport.h:515:54: note: expanded from macro 'Py_DEPRECATED'
    #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                         ^
    pygraphviz/graphviz_wrap.c:1923:7: warning: 'tp_print' is deprecated [-Wdeprecated-declarations]
          0,                                    /* tp_print */
          ^
    /Users/susiekim4/opt/anaconda3/include/python3.8/cpython/object.h:260:5: note: 'tp_print' has been explicitly marked deprecated here
        Py_DEPRECATED(3.8) int (*tp_print)(PyObject *, FILE *, int);
        ^
    /Users/susiekim4/opt/anaconda3/include/python3.8/pyport.h:515:54: note: expanded from macro 'Py_DEPRECATED'
    #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
                                                         ^
    pygraphviz/graphviz_wrap.c:2711:10: fatal error: 'graphviz/cgraph.h' file not found
    #include "graphviz/cgraph.h"
             ^~~~~~~~~~~~~~~~~~~
    2 warnings and 1 error generated.
    error: command 'gcc' failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /Users/susiekim4/opt/anaconda3/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/j5/z34rlnbj2l94p1hh0x_hkkjw0000gn/T/pip-install-zr2twxnl/pygraphviz_e78bc2c823034812a5ffbd6ef61160d7/setup.py'"'"'; __file__='"'"'/private/var/folders/j5/z34rlnbj2l94p1hh0x_hkkjw0000gn/T/pip-install-zr2twxnl/pygraphviz_e78bc2c823034812a5ffbd6ef61160d7/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/j5/z34rlnbj2l94p1hh0x_hkkjw0000gn/T/pip-record-pnja99aw/install-record.txt --single-version-externally-managed --compile --install-headers /Users/susiekim4/opt/anaconda3/include/python3.8/pygraphviz Check the logs for full command output.

I've tried pip3 install, pip install cgraph, and the other solutions.

What am I missing?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
saucykins
  • 181
  • 1
  • 3

4 Answers4

34

You can use Homebrew to install graphviz first:

brew install graphviz

Check where it was installed with brew info graphviz.

$ brew info graphviz
graphviz: stable 2.50.0 (bottled), HEAD
Graph visualization software from AT&T and Bell Labs
https://www.graphviz.org/
/usr/local/Cellar/graphviz/2.50.0 (292 files, 6.9MB) *
...

In my case, it was installed in /usr/local/Cellar/graphviz/2.50.0. Alternatively, you can run brew --prefix graphviz to get where it is installed

Then run:

export GRAPHVIZ_DIR="$(brew --prefix graphviz)"
pip install pygraphviz --global-option=build_ext --global-option="-I$GRAPHVIZ_DIR/include" --global-option="-L$GRAPHVIZ_DIR/lib"

Update: starting pip 23.3, use --config-settings instead of --global-option:

export GRAPHVIZ_DIR="$(brew --prefix graphviz)"
pip install pygraphviz --config-settings=build_ext --config-settings="-I$GRAPHVIZ_DIR/include" --config-settings="-L$GRAPHVIZ_DIR/lib"

where you set the GRAPHVIZ_DIR path and the <VERSION> with the version you got with brew info graphviz.

DanielM
  • 3,598
  • 5
  • 37
  • 53
  • 1
    If `/opt/homebrew/*` not found use `/usr/local/Cellar/*` or `/usr/local/opt/*` as the GRAPHVIZ_DIR – Shai Volvovsky Dec 28 '21 at 10:56
  • 2
    To get the GRAPHVIZ_DIR, you can do `brew info graphviz` and it should show where the package was installed. – Gino Mempin Feb 07 '22 at 08:12
  • Just for the record: This answer worked for me in June 2022, Mac OS 12.3.1, pip 22.1.2, graphviz 3.0.0, and pygraphviz 1.9. – PLL Jun 18 '22 at 08:14
  • This worked for me as well. In case you didn't use brew, you can get similar info out with `mdfind -name cgraph.h`. Which, in my case, returned that I installed it with brew after all (most of my installs are with macport): `/usr/local/Cellar/graphviz/2.50.0/include/graphviz/cgraph.h` – JL Peyret Jun 24 '22 at 23:18
  • MacOs with ARM processors: arch -x86_64 brew install graphviz – Lucian Feb 13 '23 at 14:47
  • 1
    Worked for me, but it seems it will have to be modified soon as I get a deprecation warning: `DEPRECATION: --build-option and --global-option are deprecated. pip 23.3 will enforce this behaviour change. A possible replacement is to use --config-settings.` – vlizana Aug 06 '23 at 15:51
12

For me it worked using this:

python -m pip install \
    --global-option=build_ext \
    --global-option="-I$(brew --prefix graphviz)/include/" \
    --global-option="-L$(brew --prefix graphviz)/lib/" \
    pygraphviz
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
0

I copy /opt/homebrew/bin/dot to /usr/local/bin/dot, restart the visual studio code and done!

Command:

sudo cp /opt/homebrew/bin/dot /usr/local/bin/dot
Nataniel Paiva
  • 485
  • 4
  • 9
-1

Highly recommend the only solution that worked for me from azabraoo:

pip install --global-option=build_ext --global-option="-I$(brew --prefix graphviz)/include/" --global-option="-L$(brew --prefix graphviz)/lib/" pygraphviz

Olshansky
  • 5,904
  • 8
  • 32
  • 47