Mac OS X (Yosemite) の virtualenv 環境に、PyGraphviz を pip でインストールしようしたところ、エラーとなりました。
setup.py実行してインストールしましたので、その際の手順を記載します。


エラーの内容

  • pip install 実行

    pip install pygraphviz
    

  • エラー内容

    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
            change the include_dirs and library_dirs variables in setup.py to
            point to the correct locations of your graphviz installation.
    
            The current setting of library_dirs and include_dirs is:
    library_dirs=None
    include_dirs=None
    
    error: Error locating graphviz.
    
    Your Graphviz installation could not be found. ですので、Graphviz が見つからないと言われています。
    find で graphviz を探すと以下のような結果となりました。
    graphviz は 存在しましたので virtualenv 環境だと path が通っていないようです。

  • find コマンドの結果

    % sudo find / -name graphviz
    ......
    /usr/local/Cellar/graphviz
    /usr/local/Cellar/graphviz/2.38.0/include/graphviz
    /usr/local/Cellar/graphviz/2.38.0/lib/graphviz
    /usr/local/Cellar/graphviz/2.38.0/share/graphviz
    /usr/local/include/graphviz
    /usr/local/lib/graphviz
    /usr/local/opt/graphviz
    /usr/local/share/graphviz
    /usr/local/var/homebrew/linked/graphviz
    /usr/share/file/magic/graphviz
    


参考


手順

前提

graphvizをインストールしておきます。

brew install graphviz

1. pygraphvizのソースコードを取得

PyGraphvizのサイトからソースコードをダウンロードします。
※現在の最新版はpygraphviz-1.3.1でした。

2. pygraphvizのダウンロードディレクトリにある、setup_extra.pyを編集する

setup_extra.py内にある_pkg_config()メソッド内の以下の記述を

def _pkg_config():
    # attempt to find graphviz installation with pkg-config
    # should work with modern versions of graphviz
    library_dirs=None
    include_dirs=None    
以下に修正します。

def _pkg_config():
    # attempt to find graphviz installation with pkg-config
    # should work with modern versions of graphviz
    library_path='/usr/local/lib/graphviz/'
    include_path='/usr/local/include/graphviz/'
※僕のgraphvizがインストールされたディレクトリは上記でした。
環境によっては異なるかも

3. Terminalでvirtualenvの仮想環境に入る

4. pygraphvizのダウンロードディレクトリに移動して、以下のコマンドを実行

python setup.py install


上記以外の方法はあるのか?

install - Python does not see pygraphviz - Stack Overflow見る限り、pip にオプション指定でもインストールできそうです。
ただ、試していないので失敗したらごめんなさい。

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

以上です。

コメント