python3の環境でpygraphvizをインストールしようとしてみた時のメモ
macのpython3環境でpygraphvizをインストールしようとした時のメモ
事前にmacportsでgraphviz-develをインストールしておきます。*1
sudo port install graphviz-devel
前提の環境
$ python -V Python 3.4.2
試しにpip でinstallしようとする
試しにpipでインストールしようとしてみるとpython3には対応していないらしく落ちます。
$ pip install pygraphviz Downloading/unpacking pygraphviz Running setup.py (path:~/venvs/is3/build/pygraphviz/setup.py) egg_info for package pygraphviz Traceback (most recent call last): File "<string>", line 17, in <module> File "~/venvs/is3/build/pygraphviz/setup.py", line 23 print "To install, run 'python setup.py install'" ^ SyntaxError: Missing parentheses in call to 'print' Complete output from command python setup.py egg_info: Traceback (most recent call last): File "<string>", line 17, in <module> File "~/venvs/is3/build/pygraphviz/setup.py", line 23 print "To install, run 'python setup.py install'" ^ SyntaxError: Missing parentheses in call to 'print' ---------------------------------------- Cleaning up... Command python setup.py egg_info failed with error code 1 in ~/venvs/is3/build/pygraphviz Storing debug log for failure in /Users/nao/.pip/pip.log
githubのissuesを見てみると対応しているリポジトリがある
githubのissuesを見てみると対応しているリポジトリがあるようです。
https://github.com/pygraphviz/pygraphviz/issues/16
kagamiさんという方が対応してくれたようです。windows関係ではバグレポートがまだあがっているようです。 pygraphviz1.3のmilestoneに入っているらしいので最新のreleaseを見てみることにします。
https://github.com/pygraphviz/pygraphviz/releases
以下のようなtagが打たれているようです。
- pygraphviz-1.3rc2
- pygraphviz-1.3rc1
- pygraphviz-1.2
てきとうに1.3rc2を使ってみることにします。
pip で直接githubからinstall枝葉として失敗する
pipはgitリポジトリから直接インストールする機能などを持ってたりします。 この辺りもちゃんとドキュメントに書いてあったりするので、2次的な情報ばかりに頼らない方が意外と楽なのかもしれません。
https://pip.pypa.io/en/latest/reference/pip_install.html#git
今回はgithubから指定されたtagの位置のものをインストールしたいので以下が対応しそうです。
[-e] git://git.myproject.org/MyProject.git@v1.0#egg=MyProject
githubのURLの設計に合わせると以下の様な形でinstallできそうです。
pip install -e git://github.com/pygraphviz/pygraphviz.git@pygraphviz-1.3rc2#egg=pygraphviz Obtaining pygraphviz from git+git://github.com/pygraphviz/pygraphviz.git@pygraphviz-1.3rc2#egg=pygraphviz Updating ./src/pygraphviz clone (to pygraphviz-1.3rc2) Running setup.py (path:~/venvs/is3/src/pygraphviz/setup.py) egg_info for package pygraphviz # .. skip /usr/bin/clang -Wno-unused-result -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -pipe -Os -Ib'/opt/local/include/graphviz' -I~/venvs/is3/include -I/opt/local/Library/Frameworks/Python.framework/Versions/3.4/include/python3.4m -c pygraphviz/graphviz_wrap.c -o build/temp.macosx-10.8-x86_64-3.4/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 '/usr/bin/clang' failed with exit status 1 ---------------------------------------- Cleaning up... Command ~/venvs/is3/bin/python -c "import setuptools, tokenize; __file__='~/venvs/is3/src/pygraphviz/setup.py'; exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" develop --no-deps failed with error code 1 in ~/venvs/is3/src/pygraphviz Storing debug log for failure in /Users/nao/.pip/pip.log
コンソール出力全部張りましたが。重要なのは以下だけです。
/usr/bin/clang -Wno-unused-result -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -pipe -Os -Ib'/opt/local/include/graphviz' -I~/venvs/is3/include -I/opt/local/Library/Frameworks/Python.framework/Versions/3.4/include/python3.4m -c pygraphviz/graphviz_wrap.c -o build/temp.macosx-10.8-x86_64-3.4/pygraphviz/graphviz_wrap.o pygraphviz/graphviz_wrap.c:2954:10: fatal error: 'graphviz/cgraph.h' file not found #include "graphviz/cgraph.h"
拡張ライブラリの部分をコンパイルしようとしましたが失敗したということでした。
諦めてリポジトリからソースをダウンロードして中を覗いてみる
諦めてリポジトリからソースをダウンロードして中を覗いてみます。その前に、一応インストール出来るかどうか確認してみましょう。
$ git clone git://github.com/pygraphviz/pygraphviz.git $ cd pygraphviz $ git tag --list pygraphviz-1.2 pygraphviz-1.3rc1 pygraphviz-1.3rc2 $ git co `git tag --list | tail -n 1` -b temporary python setup.py install # 同様のエラーが出ます。
先のエラーからheaderファイルが見つからないということは分かっていたので。その辺りを元にsetup.pyを見ていきます。
何やらlibrary_dirs
とinclude_dirs
の2つの変数を設定すれば良さそうということが分かりました。
# specify it here by uncommenting these lines or providing your own: # You must set both 'library_dirs' and 'include_dirs' # OSX (MacPorts) #library_dirs='/opt/local/lib/graphviz' #include_dirs='/opt/local/include/graphviz'
この設定を変えればインストール時にコンパイラに渡すオプションを変えられそうなようです。それ以前にこれらの位置を推測をしていたようなのできっとpkg-config
などを使っているでしょう。grepして調べてみることにします。
$ grep -l pkg-config * setup.py setup_extra.py
setup_extra.pyを覗いてみることにします。
pkg-config --cflags-only-I libcgraph
という表示が見つかります。どうやらlibcgraphという設定を使っていたようです。
試しに現在の環境に存在するか調べてみましょう。
$ pkg-config --list-all | grep cgraph libcgraph libcgraph - Graph library (file i/o, dot language parsing, graph, subgraph, node, edge, attribute, data structure manipulation)
ありそうです。--cflagsを付けてpathを調べてみましょう。
$ pkg-config --cflags libcgraph -I/opt/local/include/graphviz
ありますね。また、~/.pip/pip.logを覗いてみましょうこれにはpipでインストールした時のlogが書かれています。
$ grep '\(include\|library\)_dirs' ~/.pip/pip.log library_dirs=b'/opt/local/lib' include_dirs=b'/opt/local/include/graphviz' library_dirs=b'/opt/local/lib' include_dirs=b'/opt/local/include/graphviz' library_dirs=b'/opt/local/lib' include_dirs=b'/opt/local/include/graphviz'
設定はここから取ってきていたようです。 ところで実際にディレクトリを覗いてみましょう。
$ ls /opt/local/include/graphviz/ arith.h cdt.h cgraph.h # .. skip usershape.h xdot.h
cgraph.hはありますね。どうやらパスの問題だけそうです。
コンパイラ実行時のオプションの指定が以下の様になっていました。
-I /opt/local/include/graphviz
またエラー時のパスが以下の様になっていました。
pygraphviz/graphviz_wrap.c:2954:10: fatal error: 'graphviz/cgraph.h' file not found
-Iオプションで/opt/local/include/graphviz
を指定していたら、graphviz/cgraph.h
を探そうとする際には以下のパスを探そうとするように見えます。*2
/opt/local/include/graphviz/graphviz/cgraph.h
このようなファイルは存在しないのでエラーになっているわけですね。
そこでテキトウにパスが合うように変更してあげます。
$ git diff diff --git a/setup.py b/setup.py old mode 100644 new mode 100755 index 235307d..c87eac4 --- a/setup.py +++ b/setup.py @@ -46,8 +46,8 @@ define_macros = [] #include_dirs='/sw/include/graphviz' # OSX (MacPorts) -#library_dirs='/opt/local/lib/graphviz' -#include_dirs='/opt/local/include/graphviz' +library_dirs='/opt/local/lib' +include_dirs='/opt/local/include' # Windows # Unknown - use command line -I and -L switches to set
今度はinstall出来ました。
$ python setup.py install $ python examples/star.py # writing star.dot and star.png $ open star.png
実際に動きます。