ぴろの狂人日記

2014年から頑張ってブログを継続しようと思います。「継続と習慣」を今年の目標にしたので、頑張って更新を続けようと思います。おいおいはレビューや数学や認知科学などについて記事を書いていければと思っています

Graphviz/PyGraphvizのインストール

前々から使ってみたいとは思っていました。

調べてみたら以外にインストールのハードル自体は低そうなので、サクっと入れてみました。
brewとpip3を用いてインストールできるようです。
brewについてはbrew searchを実行してたくさん吐き出されるリストの中にgraphvizが確かにあることを自分で確認してみましょう。

環境は以下の通り。

  • El'Captan(10.11.5)
  • Homebrewはインストール済み

 

Graphvizのインストール

hiroshi-no-MacBook-Air:~ hiroshi$ sudo brew install graphviz


と実行するだけです。例のごとく自分のPCのパスワードを求められるので、パスワードを入力すれば、次のような処理がはしります。それほど長くない処理。

==> Downloading https://homebrew.bintray.com/bottles/graphviz-2.38.0.el_capitan.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring graphviz-2.38.0.el_capitan.bottle.1.tar.gz
🍺  /usr/local/Cellar/graphviz/2.38.0: 469 files, 67M


Graphvizのインストール自体はこれだけ。
PyGraphvizのインストール、と先へ進む前に、Graphvizのインストールがきちんとできているか確認します。

テキストファイルに次のように記述して、Sample.dotというように拡張子をdotにして保存します。

#####以下のように記述し、dotという拡張子で保存
graph g{
    "Hello World"
    }


でSample.dotという、今保存したファイルが保存してあるディレクトリへカレントディレクトリを移し、そこで次のようなコマンドを実行します。

######Sample.svgというファイルで出力する場合
hiroshi-no-MacBook-Air:Pro-tr hiroshi$ dot -Tsvg Sample.dot -o Sample.svg


そうするとSample.dotが保存されているディレクトリと同じディレクトリにSample.svgとファイルが生成されます。
それをブラウザで開けば、次のようなものが表示されます。

 

注意)紛らわしいけど、上に貼り付けてある画像はSVGファイルをブラウザで開いたものをスクリーンショットで撮影したpng画像です!BloggerだとSVGファイルは挿入できないらしい。

PyGraphvizのインストール

pip3を入れているので、以下のようにコマンドを打つだけで完了です。

hiroshi-no-MacBook-Air:Pro-tr hiroshi$ sudo pip3 install pygraphviz


例のごとく自分のPCのパスワードを入れると次のような処理が進み(すぐ終わります)、完了です。

The directory '/Users/hiroshi/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
You are using pip version 7.1.0, however version 8.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
The directory '/Users/hiroshi/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting pygraphviz
  Downloading pygraphviz-1.3.1.zip (123kB)
    100% |████████████████████████████████| 126kB 2.0MB/s 
Building wheels for collected packages: pygraphviz
  Running setup.py bdist_wheel for pygraphviz
  Stored in directory: /Users/hiroshi/Library/Caches/pip/wheels/81/03/ed/eb3b7ef51ee6527abfaf2701e222e74107b97344cfa2201881
Successfully built pygraphviz
Installing collected packages: pygraphviz
Successfully installed pygraphviz-1.3.1


確認作業は、

hiroshi-no-MacBook-Air:Pro-tr hiroshi$ python3    #python3と打ってrerurnを押す。
Python 3.4.3 (default, Aug 11 2015, 08:57:25) 
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygraphviz       #import pygraphvizと打ってreturn
>>> 


としてエラーがでなければOK。