Re: Comment on Example in python-graph

380 views
Skip to first unread message

python...@googlecode.com

unread,
Nov 7, 2009, 12:32:49 AM11/7/09
to python...@googlegroups.com
Comment by peter.liske:

Alas..I am on Windows. There is no python subdir in my GraphViz install
folder:

so what should I replace the dir in
sys.path.append('/usr/lib/graphviz/python/') with? Do I need to install
another python graphviz binding for Windows?
thanks


For more information:
http://code.google.com/p/python-graph/wiki/Example

python...@googlecode.com

unread,
Nov 10, 2009, 11:22:46 AM11/10/09
to python...@googlegroups.com
Comment by felixjesus.villanueva:

Also in linux (ubuntu and debian) fail loading gv

python...@googlecode.com

unread,
Nov 10, 2009, 11:54:16 AM11/10/09
to python...@googlegroups.com
Comment by pmatiello:

For Ubuntu, I think that you must install the libgv-python package and
append the following directory to your
sys.path: "/usr/share/python-support/libgv-python/".

I don't know about Windows.

python...@googlecode.com

unread,
Dec 14, 2009, 4:43:02 PM12/14/09
to python...@googlegroups.com
Comment by EscribanoRuben:

I use Ubuntu 9.10 and python2.6
The symbolic link /usr/lib/pyshared/python2.6/_gv is broken, but it could
fix it

sudo mv /usr/lib/pyshared/python2.6/_gv.so
/usr/lib/pyshared/python2.6/_gv.so.bak
sudo ln -s /usr/lib/pyshared/python2.6/libgv_python.so
/usr/lib/pyshared/python2.6/_gv.so

python...@googlecode.com

unread,
Mar 14, 2010, 3:11:07 PM3/14/10
to python...@googlegroups.com
Comment by adam.aviv:

On Ubuntu 9.10 with python2.6, I was getting "cannot import module _gv"
when calling import gv. The fix above doesn't work. Here is what I did:

sudo mv /usr/lib/pyshared/python2.6/_gv.so
/usr/lib/pyshared/python2.6/_gv.so.old

sudo ln -s /usr/lib/pyshared/python2.6/libgv_python.so
/usr/lib/pyshared/python2.6/_gv.so

Then in python:
>>> import sys
>>> sys.path.append('/usr/lib/pyshared/python2.6')
>>> import gv


I also want to point out that the example at the top of this page is *way*
outdated. Fortunately, the pygraph module is well documented, but the
online sample can use some nice corrections.

python...@googlecode.com

unread,
Mar 14, 2010, 3:24:37 PM3/14/10
to python...@googlegroups.com
Comment by adam.aviv:

Junk! All the gv.so should have a leading `_` (underscore), it seems the
wikimarkups stole them away. If that is the case, let me apologise to the
former poster who probably didn't notice this. Here is what you need to do,
with proper markups:


{{{
#/bin/bash
sudo mv /usr/lib/pyshared/python2.6/_gv.so
/usr/lib/pyshared/python2.6/_gv.so.old

sudo ln -s /usr/lib/pyshared/python2.6/libgv_python.so
/usr/lib/pyshared/python2.6/_gv.so
}}}


Below, is some sample code that will work, given the fix and the updates to
the pygraph modules.

{{{
#!/usr/bin/python

import sys
sys.path.append('/usr/lib/pyshared/python2.6')

import gv,pygraph


gr = pygraph.graph()
gr.add_nodes(["A","B","C","D"])
gr.add_edge("A","B")
gr.add_edge("A","C")
gr.add_edge("A","D")
gr.add_edge("B","C")

dot = pygraph.readwrite.dot.write(gr)
gvv = gv.readstring(dot)
gv.layout(gvv,"dot")
gv.render(gvv,"png","sample.png")

python...@googlecode.com

unread,
Mar 27, 2010, 12:36:07 PM3/27/10
to python...@googlegroups.com
Comment by derek.ditch:

What is the advantage of this library over networkx? I've just recently
discovered both and wasn't sure which I should choose.

python...@googlecode.com

unread,
May 6, 2010, 9:18:58 AM5/6/10
to python...@googlegroups.com
Comment by ajuanpi:

Hi,$
Thanks for the library!
I have downloaded the version 1.7.0 and run the scripts. Tests work
perfectly
Now, the exmaple given above doesn't help
It runs with the following code, can anybody confirm this or am I doing
something wrong?


{{{
from pygraph.classes.graph import graph
gr = graph()
gr.add_nodes(["A","B","C","D"])
gr.add_edge(("A","D"))
gr.add_edge(("B","C"))
gr.add_edge(("A","C"))
gr.add_edge(("A","B"))

from pygraph.readwrite import dot
dot = dot.write(gr)
gvv = gv.readstring(dot)
gv.layout(gvv,"dot")
gv.render(gvv,"png","sample.png")

}}}

Thanks

python...@googlecode.com

unread,
May 6, 2010, 9:23:02 AM5/6/10
to python...@googlegroups.com
Comment by ajuanpi:

Hi, Thanks for the library!

I have downloaded the version 1.7.0 and run the scripts. The Tests work
perfectly.
Now, the example given above doesn't. It runs with the following code.

Can anybody confirm this or am I doing something wrong? Thanks

{{{
from pygraph.classes.graph import graph
gr = graph()
gr.add_nodes(["A","B","C","D"])
gr.add_edge(("A","D"))
gr.add_edge(("B","C"))
gr.add_edge(("A","C"))
gr.add_edge(("A","B"))

from pygraph.readwrite import dot
dot = dot.write(gr)
gvv = gv.readstring(dot)
gv.layout(gvv,"dot")
gv.render(gvv,"png","sample.png")
}}}


python...@googlecode.com

unread,
May 6, 2010, 9:28:30 AM5/6/10
to python...@googlegroups.com
Comment by pmatiello:

@ajuanpi: Are you getting some error message? Can you report it here?

python...@googlecode.com

unread,
May 6, 2010, 7:36:54 PM5/6/10
to python...@googlegroups.com
Comment by ajuanpi:

When checking the original code I get
{{{
$> pychecker example.py
Processing module example (example.py)...
Caught exception importing module example:
File "/usr/lib/pymodules/python2.6/pychecker/checker.py", line 696, in
setupMainCode()
module = imp.load_module(self.moduleName, file, filename, smt)
File "example.py", line 8, in <module>()
gr = pygraph.graph()
AttributeError: 'module' object has no attribute 'graph'

Warnings...

example:1: NOT PROCESSED UNABLE TO IMPORT

}}}

Methinks looks like a tree structure problem.

With the snippet given previously, I get no problems and everything runs
smoothly.
I was checking the code of the testunits, and there I found this way of
importing the different modules

python...@googlecode.com

unread,
May 6, 2010, 7:55:52 PM5/6/10
to python...@googlegroups.com
Comment by pmatiello:

It seems that you are trying to use pygraph.graph instead of
pygraph.classes.graph.

Try importing this way:
from pygraph.classes.graph import graph

And then:
gr = graph()
(instead of gr = pygraph.graph())

python...@googlecode.com

unread,
May 6, 2010, 10:00:58 PM5/6/10
to python...@googlegroups.com
Comment by ajuanpi:

@pmatiella
That is exactly what the solution I posted before is doing.

Thanks

python...@googlecode.com

unread,
May 6, 2010, 10:05:01 PM5/6/10
to python...@googlegroups.com
Comment by ajuanpi:

@pmatiello
Yeah, that is what I am doing in the snippet I show exactly below the
example. That code works fine.
That is why I posted this new example, the one from adam.aviv doesn't work.

python...@googlecode.com

unread,
Jun 3, 2010, 9:20:26 AM6/3/10
to python...@googlegroups.com
Comment by david.froger:

don't work at all for me

python...@googlecode.com

unread,
Jun 3, 2010, 10:17:08 AM6/3/10
to python...@googlegroups.com
Comment by david.froger:

Hi,

I'm runnig Ubuntu 10.04. Here is what I did to get the first example
working. (I hope I dont' forgot any package)

sudo easy_install python-graph-core
sudo easy_install python-graph-dot
sudo apt-get install libgv-python

sudo mv /usr/lib/pyshared/python2.6/_gv.so
/usr/lib/pyshared/python2.6/_gv.so.old
sudo ln -s /usr/lib/pyshared/python2.6/libgv_python.so
/usr/lib/pyshared/python2.6/_gv.so

#!/usr/bin/python


import sys
sys.path.append('/usr/lib/pyshared/python2.6')
import gv,pygraph

gr = pygraph.graph()

gr.add_nodes(["Portugal","Spain","France","Germany","Belgium","Netherlands","Italy"])
gr.add_nodes(["Switzerland","Austria","Denmark","Poland","Czech
Republic","Slovakia","Hungary"])
gr.add_nodes(["England","Ireland","Scotland","Wales"])


gr.add_edge("Portugal", "Spain")
gr.add_edge("Spain","France")
gr.add_edge("France","Belgium")
gr.add_edge("France","Germany")
gr.add_edge("France","Italy")
gr.add_edge("Belgium","Netherlands")
gr.add_edge("Germany","Belgium")
gr.add_edge("Germany","Netherlands")
gr.add_edge("England","Wales")
gr.add_edge("England","Scotland")
gr.add_edge("Scotland","Wales")
gr.add_edge("Switzerland","Austria")
gr.add_edge("Switzerland","Germany")
gr.add_edge("Switzerland","France")
gr.add_edge("Switzerland","Italy")
gr.add_edge("Austria","Germany")
gr.add_edge("Austria","Italy")
gr.add_edge("Austria","Czech Republic")
gr.add_edge("Austria","Slovakia")
gr.add_edge("Austria","Hungary")
gr.add_edge("Denmark","Germany")
gr.add_edge("Poland","Czech Republic")
gr.add_edge("Poland","Slovakia")
gr.add_edge("Poland","Germany")
gr.add_edge("Czech Republic","Slovakia")
gr.add_edge("Czech Republic","Germany")
gr.add_edge("Slovakia","Hungary")


dot = pygraph.readwrite.dot.write(gr)


gvv = gv.readstring(dot)
gv.layout(gvv,"dot")
gv.render(gvv,"png","sample.png")

# Then, draw the breadth first search spanning tree rooted in Switzerland
st, order = pygraph.algorithms.searching.breadth_first_search(gr,
root="Switzerland")
gst = pygraph.digraph()
gst.add_spanning_tree(st)

dot = pygraph.readwrite.dot.write(gst)
gvv = gv.readstring(dot)

gv.layout(gvv,'dot')
gv.render(gvv,'png','europe-st.png')

python...@googlecode.com

unread,
Jun 14, 2010, 11:39:56 PM6/14/10
to python...@googlegroups.com
Comment by shortempered:

spent two hours on it, 64 bit machine, ubuntu 10.04 - graphviz does not
work.

get this error in above sample code:

gr.add_edge(("Portugal", "Spain"))
TypeError: add_edge() takes at least 3 arguments (2 given)

sorry, have to move to networkx

python...@googlecode.com

unread,
Jun 15, 2010, 6:41:07 AM6/15/10
to python...@googlegroups.com
Comment by tomaz.kovacic:

Looks like you're running an older version of python-graph, where add_edge
was declared like so:

def add_edge(self,u,v):
#...

if you look at our changelog
http://code.google.com/p/python-graph/wiki/Changelog there was a change in
the API design for edges since 1.6.3 release:
* Edges are now passed around as generic objects. In the case of graph /
digraph this is a tuple.

If you'll update to the latest version of python-graph, your problem is
going to be fixed.
http://code.google.com/p/python-graph/downloads/list

Regards, Tomaž

python...@googlecode.com

unread,
Jun 23, 2010, 5:59:14 PM6/23/10
to python...@googlegroups.com
Comment by Trente.Nerfs:

About the "all cycles" question, you might want to add Johnson's algorithm
(see for instance
http://stackoverflow.com/questions/546655/finding-all-cycles-in-graph/2794683#2794683)

python...@googlecode.com

unread,
Jul 27, 2010, 7:29:16 PM7/27/10
to python...@googlegroups.com
Comment by s...@janmedia.com:

Hi all, I can't find if is possible save graph in xml file using
python-graph.

i.e i have got graple like below and I need save if to xml
{{{


gr = graph()
gr.add_nodes(["A","B","C","D"])
gr.add_edge(("A","D"))
gr.add_edge(("B","C"))
gr.add_edge(("A","C"))
gr.add_edge(("A","B"))
}}}

If is possible please give me example.

python...@googlecode.com

unread,
Oct 7, 2010, 3:15:52 AM10/7/10
to python...@googlegroups.com
Comment by kiran.shesha1:

all the above are wrong dont waste time

python...@googlecode.com

unread,
Apr 7, 2011, 3:25:37 AM4/7/11
to python...@googlegroups.com
Comment by bebsoui...@gmail.com:

Hi !
I'm working on Windows with Python 2.7.

I try your code and I have the error :
import gv
ImportError: No module named gv

I try to find the module gv for a few hours, I found graphviz but it still
doesn't work...

Can you help me please ?

python...@googlecode.com

unread,
Apr 20, 2011, 10:26:21 AM4/20/11
to python...@googlegroups.com
Comment by dhara...@gmail.com:

So: I'm puzzled why I can't find this package's definition for the "node"
object. Is it because:
1) The documentation generator is overlooking it. Or:
2) There is no "node" class defined in this package.

If #2, then what is allowable as a node?
a) A string.
b) Strings, ints, floats.... any of Python's "scalar" values.
c) Any object.
d) Any object that would be suitable as a key in a dictionary.
e) Any object, as long as it is .... hashable? immutable? implements a
__cmp__() method?

It is quite possible I'm mixing the notion of "node" and "node
identifier". If so... please untangle me.

Thanks -- DJH

python...@googlecode.com

unread,
Apr 20, 2011, 11:15:48 AM4/20/11
to python...@googlegroups.com
Comment by pmatiello:

There's no node class, indeed. Any object that can be used as a dictionary
key can be used as a node. Avoid mutability in these objects (node objects
are stored internally as dictionary keys, so if they are modified they
might not be found later, corrupting the data structure).

This might be a clarity problem on the documentation. If you like, you may
file a issue requesting an improvement on these matters.

python...@googlecode.com

unread,
May 15, 2011, 9:04:47 PM5/15/11
to python...@googlegroups.com
Comment by manch...@gmail.com:

I have a doubt as below:

=====
gr = graph()
gr.add_node('1')
gr.add_node('2')
e = ('1', '2')
gr.add_edge(e, wt=3.0)

for e in gr.edges():
w = 1+gr.edge_weight(e)
gr.set_edge_weight(e,wt=round(w,3))

=====

Then the weight for edge e turns into 5 but not 4. Since it treat ('1','2')
as an edge while ('2','1') as another one. How can it fix this? Thanks!!

python...@googlecode.com

unread,
May 15, 2011, 9:08:47 PM5/15/11
to python...@googlegroups.com
Comment by manch...@gmail.com:

I have a doubt as below:

{{{


gr = graph()
gr.add_node('1')
gr.add_node('2')
e = ('1', '2')
gr.add_edge(e, wt=3.0)

for e in gr.edges():
w = 1+gr.edge_weight(e)
gr.set_edge_weight(e,wt=round(w,3))
}}}

Then the weight for edge e turns into 5 but not 4. Since it treat ('1','2')

python...@googlecode.com

unread,
Aug 31, 2011, 1:07:21 PM8/31/11
to python...@googlegroups.com
Comment by stephane...@gmail.com:

Hello,
Thank your very much for this module !

I would like to get the list of all possible linkpath,
Eg. from:
# Add nodes and edges
gr.add_nodes(["A","B","C","D","E"])

gr.add_edge(("A", "B"))
gr.add_edge(("B", "A"))
gr.add_edge(("A", "C"))
gr.add_edge(("C", "D"))
gr.add_edge(("B", "E"))

# Draw as PNG
dot = write(gr)
gvv = gv.readstring(dot)
gv.layout(gvv,'dot')
gv.render(gvv,'png','full.png')

# Then, draw the breadth first search spanning tree rooted in
Switzerland
st, order = breadth_first_search(gr, root="B")
gst = digraph()
gst.add_spanning_tree(st)

dot = write(gst)
gvv = gv.readstring(dot)

gv.layout(gvv,'dot')
gv.render(gvv,'png','full-B.png')


I would like to get:
(["B","A","C","D"],
["B","E"])

Is that possible with python-graph ??
Many thanks,
Stéphane

python...@googlecode.com

unread,
Aug 31, 2011, 1:11:36 PM8/31/11
to python...@googlegroups.com

python...@googlecode.com

unread,
Aug 8, 2012, 9:00:39 AM8/8/12
to python...@googlegroups.com
Comment by achristo...@gmail.com:

Could you add a few words about when pythongraph would be more appropriate
than networkx ?

For more information:
https://code.google.com/p/python-graph/wiki/Example

python...@googlecode.com

unread,
Nov 25, 2012, 1:46:54 AM11/25/12
to python...@googlegroups.com
Comment by raxitshe...@gmail.com:

Can anyone put example on how to search shortest path between two nodes?
I have tried few simple stuff, but i think more doc will be much more
useful.

python...@googlecode.com

unread,
Nov 26, 2012, 8:37:45 AM11/26/12
to python...@googlegroups.com
Comment by raxitshe...@gmail.com:

#import necessary stuff here

g.add_nodes(["a","b","c","d","e","f"])
g.add_edge(("a","b"))
g.add_edge(("a","c"))
g.add_edge(("b","c"))
hs=heuristic_search(g,"a","c",chow.shortest_path)

In [30]: hs=heuristic_search(g,"a","c",chow.shortest_path)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-30-af0c33ee9e01> in <module>()
----> 1 hs=heuristic_search(g,"a","c",chow.shortest_path)

/usr/local/lib/python2.7/dist-packages/python_graph_core-1.8.2-py2.7.egg/pygraph/algorithms/minmax.pyc
in
heuristic_search(graph, start, goal, heuristic)
303 # it when we visit the node a second time.
304 else:
--> 305 h = heuristic(neighbor, goal)
306
307 g[neighbor] = ncost, h

/usr/local/lib/python2.7/dist-packages/python_graph_core-1.8.2-py2.7.egg/pygraph/algorithms/minmax.pyc
in
shortest_path(graph, source)
175 if u not in finished:
176 finished.add(u)
--> 177 for v in graph[u]:
178 if v not in finished:
179 alt = du + graph.edge_weight((u, v))

TypeError: string indices must be integers, not str

python...@googlecode.com

unread,
Feb 21, 2013, 7:40:26 AM2/21/13
to python...@googlegroups.com
Comment by degtua...@gmail.com:

draw with style attrs not work for directed graph.

example:
gr.add_node(node,attrs=[("style","filled"), ("fillcolor","green")])
st, order = breadth_first_search(gr, root=node_root)
gst = digraph()
gst.add_spanning_tree(st)
dot = write(gst)

gvv = gv.readstring(dot)
gv.layout(gvv,'dot')
gv.render(gvv,'png','top-st.png')

Node attrs are ignored.
Help !!! )

python...@googlecode.com

unread,
Mar 21, 2013, 8:14:32 AM3/21/13
to python...@googlegroups.com
Comment by lykhenko...@gmail.com:

I have installed all required libraries. No errors. But **nothing renders**
either. I`m trying to launch from console. Could you give me the hint?

python...@googlecode.com

unread,
Mar 21, 2013, 8:15:32 AM3/21/13
to python...@googlegroups.com

python...@googlecode.com

unread,
Apr 15, 2013, 9:36:33 AM4/15/13
to python...@googlegroups.com
Comment by pne...@unife.it:

Currently the
link "http://www.linux.ime.usp.br/~matiello/python-graph/docs/" (API
documentation) is unreachable.

Cheers

python...@googlecode.com

unread,
Apr 15, 2013, 9:47:55 AM4/15/13
to python...@googlegroups.com
Comment by pne...@unife.it:

from API documentation
breadth_first_search(graph, root=None, filter=null())

I may believe I can set the "find" filter, instantiated with a "target"
node (f=find(target_node)):
breadth_first_search(gra, source, f)
in order to compute the shortest path between source-target. Is this usage
too stupid??

Thanks

python...@googlecode.com

unread,
Apr 18, 2013, 9:07:31 PM4/18/13
to python...@googlegroups.com
Comment by pmatiello:

API documentation link is fixed now. Thanks for reporting!

python...@googlecode.com

unread,
Nov 8, 2013, 11:08:59 PM11/8/13
to python...@googlegroups.com
Comment by ahmed.el...@gmail.com:

I too, have this following error:

Traceback (most recent call last):
File "basic-graph.py", line 11, in <module>
import gv
ImportError: No module named gv

python...@googlecode.com

unread,
Feb 10, 2014, 8:06:58 AM2/10/14
to python...@googlegroups.com
Comment by Thomasny...@gmail.com:

You can use pydot instead of gv on Windows.
i.e. do following changes (after installing pydot)

import pydot
#import gv
...

graph=pydot.graph_from_dot_data(dot)
graph.write_png('europe.png')
#gvv = gv.readstring(dot)
#gv.layout(gvv,'dot')
#gv.render(gvv,'png','europe.png')

python...@googlecode.com

unread,
Feb 10, 2014, 8:09:49 AM2/10/14
to python...@googlegroups.com

python...@googlecode.com

unread,
Feb 10, 2014, 8:11:49 AM2/10/14
to python...@googlegroups.com
Comment by Thomasny...@gmail.com:

You can use pydot instead of gv on Windows. i.e. do following changes
(after installing pydot)

{{{
import pydot #import gv
}}}
{{{

python...@googlecode.com

unread,
Feb 10, 2014, 8:12:49 AM2/10/14
to python...@googlegroups.com
Comment by Thomasny...@gmail.com:

You can use pydot instead of gv on Windows. i.e. do following changes
(after installing pydot)

{{{
import pydot
#import gv
}}}
...

python...@googlecode.com

unread,
Nov 25, 2014, 7:39:45 AM11/25/14
to python...@googlegroups.com
Comment by maayan.d...@gmail.com:

Hi,

The API link ins't working :(

Maayan
Reply all
Reply to author
Forward
0 new messages