When I tried, http://networkx.lanl.gov/ was not available. I used http://networkx.github.io to read documents instead.
For installation, I dis as follows. sudo was required because of authorization problem.
$ sudo pip install networkx Password: Downloading/unpacking networkx Downloading networkx-1.7.zip (1.0MB): 1.0MB downloaded Running setup.py egg_info for package networkx warning: no files found matching 'scripts/*' warning: no files found matching 'networkx/tests/*.txt' warning: no files found matching 'networkx/*/*/tests/*.txt' 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' no previously-included directories found matching 'doc/source/reference/generated' no previously-included directories found matching 'doc/source/examples' no previously-included directories found matching 'doc/source/static/examples' no previously-included directories found matching 'doc/source/templates/gallery.html' Installing collected packages: networkx Running setup.py install for networkx deleting networkx.egg-info/requires.txt warning: no files found matching 'scripts/*' warning: no files found matching 'networkx/tests/*.txt' warning: no files found matching 'networkx/*/*/tests/*.txt' 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' no previously-included directories found matching 'doc/source/reference/generated' no previously-included directories found matching 'doc/source/examples' no previously-included directories found matching 'doc/source/static/examples' no previously-included directories found matching 'doc/source/templates/gallery.html' Successfully installed networkx Cleaning up... $
Write following code and saved as name ‘exnetworkx.py’.
import networkx as nx import matplotlib from nltk.corpus import wordnet as wn def traverse(graph, start, node): graph.depth[node.name] = node.shortest_path_distance(start) for child in node.hyponyms(): graph.add_edge(node.name, child.name) traverse(graph, start, child) def hyponym_graph(start): G = nx.Graph() G.depth = {} traverse(G, start, start) return G def graph_draw(graph): nx.draw_graphviz(graph, node_size = [16 * graph.degree(n) for n in graph], node_color = [graph.depth[n] for n in graph], with_labels = False) matplotlib.pyplot.show()
Then try to draw a graph.
>>> import exnetworkx >>> from exnetworkx import * >>> dog = wn.synset('dog.n.01') >>> graph = hyponym_graph(dog) >>> graph_draw(graph) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/ken/Documents/workspace/NLTK Learning/scripts/exnetworkx.py", line 21, in graph_draw with_labels = False) File "/Library/Python/2.7/site-packages/networkx/drawing/nx_pylab.py", line 888, in draw_graphviz pos=nx.drawing.graphviz_layout(G,prog) File "/Library/Python/2.7/site-packages/networkx/drawing/nx_agraph.py", line 229, in graphviz_layout return pygraphviz_layout(G,prog=prog,root=root,args=args) File "/Library/Python/2.7/site-packages/networkx/drawing/nx_agraph.py", line 260, in pygraphviz_layout '(not available for Python3)') ImportError: ('requires pygraphviz ', 'http://networkx.lanl.gov/pygraphviz ', '(not available for Python3)') >>>
It seems ‘pygraphviz” is required. Let’s install via pip.
$ pip install pygraphviz Downloading/unpacking pygraphviz Running setup.py egg_info for package pygraphviz /bin/sh: pkg-config: command not found /bin/sh: pkg-config: command not found Trying pkg-config Trying dotneato-config Failed to find dotneato-config 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_path and library_path variables in setup.py to point to the correct locations of your graphviz installation. The current setting of library_path and include_path is: library_path=None include_path=None Traceback (most recent call last): File "<string>", line 16, in <module> File "/private/var/folders/z_/45w_7yf1701gxyghp070g5t40000gn/T/pip-build-ken/pygraphviz/setup.py", line 89, in <module> raise OSError,"Error locating graphviz." OSError: Error locating graphviz. Complete output from command python setup.py egg_info: /bin/sh: pkg-config: command not found /bin/sh: pkg-config: command not found Trying pkg-config Trying dotneato-config Failed to find dotneato-config 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_path and library_path variables in setup.py to point to the correct locations of your graphviz installation. The current setting of library_path and include_path is: library_path=None include_path=None Traceback (most recent call last): File "<string>", line 16, in <module> File "/private/var/folders/z_/45w_7yf1701gxyghp070g5t40000gn/T/pip-build-ken/pygraphviz/setup.py", line 89, in <module> raise OSError,"Error locating graphviz." OSError: Error locating graphviz. ---------------------------------------- Command python setup.py egg_info failed with error code 1 in /private/var/folders/z_/45w_7yf1701gxyghp070g5t40000gn/T/pip-build-ken/pygraphviz Storing complete log in /var/folders/z_/45w_7yf1701gxyghp070g5t40000gn/T/tmprlcUBw $
Another error occurred. Graphviz is also required and it can be downloaded via http://www.graphviz.org/. One strange thing is I still got an error after installing Graphviz.
I searched and found missing pkg-config. Install it with brew.
$ brew install pkg-config ==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/pkg-config- ######################################################################## 100.0% ==> Pouring pkg-config-0.28.mountain_lion.bottle.tar.gz 🍺 /usr/local/Cellar/pkg-config/0.28: 10 files, 636K
Still got error but different from the last one.
$ pip install pygraphviz Downloading/unpacking pygraphviz Running setup.py egg_info for package pygraphviz Trying pkg-config library_path=/usr/local/lib include_path=/usr/local/include/graphviz 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' Installing collected packages: pygraphviz Running setup.py install for pygraphviz Trying pkg-config library_path=/usr/local/lib include_path=/usr/local/include/graphviz building 'pygraphviz._graphviz' extension clang -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch x86_64 -pipe -I/usr/local/include/graphviz -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c pygraphviz/graphviz_wrap.c -o build/temp.macosx-10.8-intel-2.7/pygraphviz/graphviz_wrap.o unable to execute clang: No such file or directory error: command 'clang' failed with exit status 1 Complete output from command /usr/bin/python -c "import setuptools;__file__='/private/var/folders/z_/45w_7yf1701gxyghp070g5t40000gn/T/pip-build-ken/pygraphviz/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/z_/45w_7yf1701gxyghp070g5t40000gn/T/pip-ej0jLZ-record/install-record.txt --single-version-externally-managed: Trying pkg-config library_path=/usr/local/lib include_path=/usr/local/include/graphviz running install running build running build_py creating build creating build/lib.macosx-10.8-intel-2.7 creating build/lib.macosx-10.8-intel-2.7/pygraphviz copying pygraphviz/__init__.py -> build/lib.macosx-10.8-intel-2.7/pygraphviz copying pygraphviz/agraph.py -> build/lib.macosx-10.8-intel-2.7/pygraphviz copying pygraphviz/graphviz.py -> build/lib.macosx-10.8-intel-2.7/pygraphviz copying pygraphviz/release.py -> build/lib.macosx-10.8-intel-2.7/pygraphviz copying pygraphviz/version.py -> build/lib.macosx-10.8-intel-2.7/pygraphviz creating build/lib.macosx-10.8-intel-2.7/pygraphviz/tests copying pygraphviz/tests/__init__.py -> build/lib.macosx-10.8-intel-2.7/pygraphviz/tests copying pygraphviz/tests/test.py -> build/lib.macosx-10.8-intel-2.7/pygraphviz/tests copying pygraphviz/tests/attributes.txt -> build/lib.macosx-10.8-intel-2.7/pygraphviz/tests copying pygraphviz/tests/graph.txt -> build/lib.macosx-10.8-intel-2.7/pygraphviz/tests copying pygraphviz/tests/layout_draw.txt -> build/lib.macosx-10.8-intel-2.7/pygraphviz/tests copying pygraphviz/tests/unicode.txt -> build/lib.macosx-10.8-intel-2.7/pygraphviz/tests running build_ext building 'pygraphviz._graphviz' extension creating build/temp.macosx-10.8-intel-2.7 creating build/temp.macosx-10.8-intel-2.7/pygraphviz clang -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch x86_64 -pipe -I/usr/local/include/graphviz -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c pygraphviz/graphviz_wrap.c -o build/temp.macosx-10.8-intel-2.7/pygraphviz/graphviz_wrap.o unable to execute clang: No such file or directory error: command 'clang' failed with exit status 1 ---------------------------------------- Command /usr/bin/python -c "import setuptools;__file__='/private/var/folders/z_/45w_7yf1701gxyghp070g5t40000gn/T/pip-build-ken/pygraphviz/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/z_/45w_7yf1701gxyghp070g5t40000gn/T/pip-ej0jLZ-record/install-record.txt --single-version-externally-managed failed with error code 1 in /private/var/folders/z_/45w_7yf1701gxyghp070g5t40000gn/T/pip-build-ken/pygraphviz Storing complete log in /var/folders/z_/45w_7yf1701gxyghp070g5t40000gn/T/tmpzp_ZPZ
Google is a great reacher. I found a report the same error was avoided by installing Command line tool of XCode.
$ easy_install pygraphviz Searching for pygraphviz Reading http://pypi.python.org/simple/pygraphviz/ Reading http://networkx.lanl.gov/pygraphviz Reading http://networkx.lanl.gov/download/pygraphviz Reading http://networkx.lanl.gov/wiki/download Reading http://networkx.lanl.gov/download Reading http://sourceforge.net/project/showfiles.php?group_id=122233&package_id=161979 Best match: pygraphviz 1.1 Downloading http://networkx.lanl.gov/download/pygraphviz/pygraphviz-1.1.tar.gz Processing pygraphviz-1.1.tar.gz Running pygraphviz-1.1/setup.py -q bdist_egg --dist-dir /var/folders/z_/45w_7yf1701gxyghp070g5t40000gn/T/easy_install-ksFhOM/pygraphviz-1.1/egg-dist-tmp-oKMDy2 Trying pkg-config library_path=/usr/local/lib include_path=/usr/local/include/graphviz 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' clang: warning: argument unused during compilation: '-mno-fused-madd' pygraphviz/graphviz_wrap.c:2040:11: warning: explicitly assigning a variable of type 'int' to itself [-Wself-assign] res = SWIG_AddCast(res); ~~~ ^ ~~~ pygraphviz/graphviz_wrap.c:2043:11: warning: explicitly assigning a variable of type 'int' to itself [-Wself-assign] res = SWIG_AddCast(res); ~~~ ^ ~~~ pygraphviz/graphviz_wrap.c:2855:12: warning: incompatible pointer to integer conversion returning 'Agsym_t *' (aka 'struct Agsym_s *') from a function with result type 'int' [-Wint-conversion] return agattr(g, kind, name, val); ^~~~~~~~~~~~~~~~~~~~~~~~~~ pygraphviz/graphviz_wrap.c:4806:14: warning: explicitly assigning a variable of type 'void *' to itself [-Wself-assign] clientdata = clientdata; ~~~~~~~~~~ ^ ~~~~~~~~~~ 4 warnings generated. pygraphviz/graphviz_wrap.c:2040:11: warning: explicitly assigning a variable of type 'int' to itself [-Wself-assign] res = SWIG_AddCast(res); ~~~ ^ ~~~ pygraphviz/graphviz_wrap.c:2043:11: warning: explicitly assigning a variable of type 'int' to itself [-Wself-assign] res = SWIG_AddCast(res); ~~~ ^ ~~~ pygraphviz/graphviz_wrap.c:2829:15: warning: implicit conversion loses integer precision: 'size_t' (aka 'unsigned long') to 'int' [-Wshorten-64-to-32] len = strlen(val); ~ ^~~~~~~~~~~ pygraphviz/graphviz_wrap.c:2855:12: warning: incompatible pointer to integer conversion returning 'Agsym_t *' (aka 'struct Agsym_s *') from a function with result type 'int' [-Wint-conversion] return agattr(g, kind, name, val); ^~~~~~~~~~~~~~~~~~~~~~~~~~ pygraphviz/graphviz_wrap.c:2847:15: warning: implicit conversion loses integer precision: 'size_t' (aka 'unsigned long') to 'int' [-Wshorten-64-to-32] len = strlen(val); ~ ^~~~~~~~~~~ pygraphviz/graphviz_wrap.c:4806:14: warning: explicitly assigning a variable of type 'void *' to itself [-Wself-assign] clientdata = clientdata; ~~~~~~~~~~ ^ ~~~~~~~~~~ 6 warnings generated. ld: warning: ld: warning: ignoring file /usr/local/lib/libcgraph.dylib, file was built for unsupported file format ( 0xcf 0xfa 0xed 0xfe 0x 7 0x 0 0x 0 0x 1 0x 3 0x 0 0x 0 0x 0 0x 6 0x 0 0x 0 0x 0 ) which is not the architecture being linked (i386): /usr/local/lib/libcgraph.dylibignoring file /usr/local/lib/libcdt.dylib, file was built for unsupported file format ( 0xcf 0xfa 0xed 0xfe 0x 7 0x 0 0x 0 0x 1 0x 3 0x 0 0x 0 0x 0 0x 6 0x 0 0x 0 0x 0 ) which is not the architecture being linked (i386): /usr/local/lib/libcdt.dylib zip_safe flag not set; analyzing archive contents... pygraphviz.release: module references __file__ pygraphviz.tests.test: module references __file__ Adding pygraphviz 1.1 to easy-install.pth file Installed /Library/Python/2.7/site-packages/pygraphviz-1.1-py2.7-macosx-10.8-intel.egg Processing dependencies for pygraphviz Finished processing dependencies for pygraphviz
Seems succeeded although some warnings were triggered.
Before retry, add one line “import pygraphviz” to avoid unexpected error.
import networkx as nx import matplotlib <strong>import pygraphviz</strong> from nltk.corpus import wordnet as wn ....
Try try try!!!
>>> import pygraphviz >>> import exnetworkx >>> from exnetworkx import * >>> dog = wn.synset('dog.n.01') >>> graph = hyponym_graph(dog) >>> graph_draw(graph)
Finally done. It took a half day…