如何在 Python 3.6 上使用 conda 或 pip 安装 pygraphviz?

How to install pygraphviz using conda or pip on Python 3.6?

我不确定在这里做什么,但我确实需要将 graphviz 布局与 networkx 一起使用。

我是按照找不到graphviz的安装过程的具体方法来做的:

Your Graphviz installation could not be found.

        1) You don't have Graphviz installed:
           Install Graphviz (http://graphviz.org)

        2) Your Graphviz package might incomplete.
           Install the binary development subpackage (e.g. libgraphviz-dev or similar.)

        3) You are using Windows
           There are no PyGraphviz binary packages for Windows but you might be
           able to build it from this source.  See
           http://networkx.lanl.gov/pygraphviz/reference/faq.html

        If you think your installation is correct you will need to manually
        provide path to graphviz include and library. For example:

        pip install pygraphviz --install-option="--include-path=/usr/include/graphviz" --install-option="--library-path=/usr/lib/graphviz/"

        The current setting of library_dirs and include_dirs is:
library_dirs=None
include_dirs=None

error: Error locating graphviz.

这是我的 python 版本:

(py3_testing) jespinozlt-osx:~ jespinoz$ python
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 12:04:33)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

这是我 Mac 上的 OS 版本:

(py3_testing) jespinozlt-osx:~ jespinoz$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.11.6
BuildVersion:   15G1217

我确定 graphviz 已安装:

(py3_testing) jespinozlt-osx:~ jespinoz$ ls -l ~/anaconda/envs/py3_testing/include/| grep "graphviz"
drwxr-xr-x   29 jespinoz  tigr     986 Jun  6 12:51 graphviz
(py3_testing) jespinozlt-osx:~ jespinoz$ ls -l ~/anaconda/envs/py3_testing/lib/| grep "graphviz"
drwxr-xr-x   18 jespinoz  tigr       612 Jun  6 12:51 graphviz

现在,当我尝试通过 pip 安装时:

(py3_testing) jespinozlt-osx:~ jespinoz$ pip install pygraphviz --install-option="--include-path=~/anaconda/envs/py3_testing/include/graphviz/" --install-option="--library-path=~/anaconda/envs/py3_testing/lib/graphviz"
/Users/jespinoz/anaconda/envs/py3_testing/lib/python3.6/site-packages/pip/commands/install.py:194: UserWarning: Disabling all use of wheels due to the use of --build-options / --global-options / --install-options.
  cmdoptions.check_install_build_global(options)
Collecting pygraphviz
  Using cached https://files.pythonhosted.org/packages/98/bb/a32e33f7665b921c926209305dde66fe41003a4ad934b10efb7c1211a419/pygraphviz-1.3.1.tar.gz
Skipping bdist_wheel for pygraphviz, due to binaries being disabled for it.
Installing collected packages: pygraphviz
  Running setup.py install for pygraphviz ... error
    Complete output from command /Users/jespinoz/anaconda/envs/py3_testing/bin/python -u -c "import setuptools, tokenize;__file__='/private/var/folders/6z/5vbtz_gmkr76ftgc3149dvtr0003c0/T/pip-build-_hho1hi2/pygraphviz/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /var/folders/6z/5vbtz_gmkr76ftgc3149dvtr0003c0/T/pip-v2gpw_h3-record/install-record.txt --single-version-externally-managed --compile --include-path=~/anaconda/envs/py3_testing/include/graphviz/ --library-path=~/anaconda/envs/py3_testing/lib/graphviz:
    running install
    running build
    running build_py
    creating build
    creating build/lib.macosx-10.9-x86_64-3.6
    creating build/lib.macosx-10.9-x86_64-3.6/pygraphviz
    copying pygraphviz/__init__.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz
    copying pygraphviz/agraph.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz
    copying pygraphviz/graphviz.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz
    copying pygraphviz/release.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz
    copying pygraphviz/version.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz
    creating build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/__init__.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test_attribute_defaults.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test_attributes.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test_clear.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test_drawing.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test_edge_attributes.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test_graph.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test_html.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test_layout.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test_node_attributes.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test_readwrite.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test_string.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test_subgraph.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test_unicode.py -> build/lib.macosx-10.9-x86_64-3.6/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 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.6/pygraphviz
    copying pygraphviz/graphviz_wrap.c -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz
    running build_ext
    building 'pygraphviz._graphviz' extension
    creating build/temp.macosx-10.9-x86_64-3.6
    creating build/temp.macosx-10.9-x86_64-3.6/pygraphviz
    x86_64-apple-darwin13.4.0-clang -DNDEBUG -fwrapv -O2 -Wall -Wstrict-prototypes -march=core2 -mtune=haswell -mssse3 -ftree-vectorize -fPIC -fPIE -fstack-protector-strong -O2 -pipe -D_FORTIFY_SOURCE=2 -mmacosx-version-min=10.9 -I~/anaconda/envs/py3_testing/include/graphviz/ -I/Users/jespinoz/anaconda/envs/py3_testing/include/python3.6m -c pygraphviz/graphviz_wrap.c -o build/temp.macosx-10.9-x86_64-3.6/pygraphviz/graphviz_wrap.o
    pygraphviz/graphviz_wrap.c:2954:10: fatal error: 'graphviz/cgraph.h' file not found
    #include "graphviz/cgraph.h"
             ^~~~~~~~~~~~~~~~~~~
    1 error generated.
    error: command 'x86_64-apple-darwin13.4.0-clang' failed with exit status 1

    ----------------------------------------
Command "/Users/jespinoz/anaconda/envs/py3_testing/bin/python -u -c "import setuptools, tokenize;__file__='/private/var/folders/6z/5vbtz_gmkr76ftgc3149dvtr0003c0/T/pip-build-_hho1hi2/pygraphviz/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /var/folders/6z/5vbtz_gmkr76ftgc3149dvtr0003c0/T/pip-v2gpw_h3-record/install-record.txt --single-version-externally-managed --compile --include-path=~/anaconda/envs/py3_testing/include/graphviz/ --library-path=~/anaconda/envs/py3_testing/lib/graphviz" failed with error code 1 in /private/var/folders/6z/5vbtz_gmkr76ftgc3149dvtr0003c0/T/pip-build-_hho1hi2/pygraphviz/

我安装了 Homebrew:

(python3) jespinozlt-osx:anaconda jespinoz$ ls /usr/local/Cellar/graphviz/2.40.1/include/graphviz/
(python3) jespinozlt-osx:anaconda jespinoz$ ls /usr/local/Cellar/graphviz/2.40.1/lib/
graphviz/             libcdt.dylib          libcgraph.dylib       libgvc.dylib          libgvpr.dylib         liblab_gamut.dylib    libpathplan.dylib     libxdot.dylib
libcdt.5.dylib        libcgraph.6.dylib     libgvc.6.dylib        libgvpr.2.dylib       liblab_gamut.1.dylib  libpathplan.4.dylib   libxdot.4.dylib       pkgconfig/
(python3) jespinozlt-osx:anaconda jespinoz$ ls /usr/local/Cellar/graphviz/2.40.1/lib/graphviz/
config6                 libgvplugin_core.dylib          libgvplugin_dot_layout.dylib        libgvplugin_gd.dylib            libgvplugin_neato_layout.dylib      libgvplugin_quartz.dylib
libgvplugin_core.6.dylib        libgvplugin_dot_layout.6.dylib      libgvplugin_gd.6.dylib          libgvplugin_neato_layout.6.dylib    libgvplugin_quartz.6.dylib      tcl
(python3) jespinozlt-osx:anaconda jespinoz$ pip install pygraphviz --install-option="--include-path=/usr/local/Cellar/graphviz/2.40.1/include/graphviz/" --install-option="--library-path=/usr/local/Cellar/graphviz/2.40.1/lib/graphviz"
/Users/jespinoz/anaconda/envs/python3/lib/python3.6/site-packages/pip/_internal/commands/install.py:199: UserWarning: Disabling all use of wheels due to the use of --build-options / --global-options / --install-options.
  cmdoptions.check_install_build_global(options)
Collecting pygraphviz
  Using cached https://files.pythonhosted.org/packages/98/bb/a32e33f7665b921c926209305dde66fe41003a4ad934b10efb7c1211a419/pygraphviz-1.3.1.tar.gz
Skipping bdist_wheel for pygraphviz, due to binaries being disabled for it.
mxnet 1.1.0 has requirement numpy<=1.13.3, but you'll have numpy 1.14.2 which is incompatible.
Installing collected packages: pygraphviz
  Running setup.py install for pygraphviz ... error
    Complete output from command /Users/jespinoz/anaconda/envs/python3/bin/python -u -c "import setuptools, tokenize;__file__='/private/var/folders/6z/5vbtz_gmkr76ftgc3149dvtr0003c0/T/pip-install-2d1lydtv/pygraphviz/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/6z/5vbtz_gmkr76ftgc3149dvtr0003c0/T/pip-record-adm5qrum/install-record.txt --single-version-externally-managed --compile --include-path=/usr/local/Cellar/graphviz/2.40.1/include/graphviz/ --library-path=/usr/local/Cellar/graphviz/2.40.1/lib/graphviz:
    running install
    running build
    running build_py
    creating build
    creating build/lib.macosx-10.9-x86_64-3.6
    creating build/lib.macosx-10.9-x86_64-3.6/pygraphviz
    copying pygraphviz/__init__.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz
    copying pygraphviz/agraph.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz
    copying pygraphviz/graphviz.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz
    copying pygraphviz/release.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz
    copying pygraphviz/version.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz
    creating build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/__init__.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test_attribute_defaults.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test_attributes.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test_clear.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test_drawing.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test_edge_attributes.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test_graph.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test_html.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test_layout.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test_node_attributes.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test_readwrite.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test_string.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test_subgraph.py -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz/tests
    copying pygraphviz/tests/test_unicode.py -> build/lib.macosx-10.9-x86_64-3.6/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 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.6/pygraphviz
    copying pygraphviz/graphviz_wrap.c -> build/lib.macosx-10.9-x86_64-3.6/pygraphviz
    running build_ext
    building 'pygraphviz._graphviz' extension
    creating build/temp.macosx-10.9-x86_64-3.6
    creating build/temp.macosx-10.9-x86_64-3.6/pygraphviz
    x86_64-apple-darwin13.4.0-clang -DNDEBUG -fwrapv -O2 -Wall -Wstrict-prototypes -march=core2 -mtune=haswell -mssse3 -ftree-vectorize -fPIC -fPIE -fstack-protector-strong -O2 -pipe -D_FORTIFY_SOURCE=2 -mmacosx-version-min=10.9 -I/usr/local/Cellar/graphviz/2.40.1/include/graphviz/ -I/Users/jespinoz/anaconda/envs/python3/include/python3.6m -c pygraphviz/graphviz_wrap.c -o build/temp.macosx-10.9-x86_64-3.6/pygraphviz/graphviz_wrap.o
    pygraphviz/graphviz_wrap.c:2954:10: fatal error: 'graphviz/cgraph.h' file not found
    #include "graphviz/cgraph.h"
             ^~~~~~~~~~~~~~~~~~~
    1 error generated.
    error: command 'x86_64-apple-darwin13.4.0-clang' failed with exit status 1

    ----------------------------------------
Command "/Users/jespinoz/anaconda/envs/python3/bin/python -u -c "import setuptools, tokenize;__file__='/private/var/folders/6z/5vbtz_gmkr76ftgc3149dvtr0003c0/T/pip-install-2d1lydtv/pygraphviz/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/6z/5vbtz_gmkr76ftgc3149dvtr0003c0/T/pip-record-adm5qrum/install-record.txt --single-version-externally-managed --compile --include-path=/usr/local/Cellar/graphviz/2.40.1/include/graphviz/ --library-path=/usr/local/Cellar/graphviz/2.40.1/lib/graphviz" failed with error code 1 in /private/var/folders/6z/5vbtz_gmkr76ftgc3149dvtr0003c0/T/pip-install-2d1lydtv/pygraphviz/

使用以下命令行(你需要homebrew):

brew install graphviz

终于成功了:

# PyGraphViz
conda install graphviz --yes
pip download git+git://github.com/pygraphviz/pygraphviz.git#egg=pygraphviz
unzip pygraphviz*.zip
cd pygraphviz
python setup.py install --include-path=$CONDA_PREFIX/include --library-path=$CONDA_PREFIX/lib
pip install pydot