1

I'm trying to build an application to visualize a network in plotly. However, my pygraphviz installation seems to be giving me trouble. Every time I try to run code, it gives me the error "AttributeError: module 'pygraphviz' has no attribute 'AGraph'"

I'm using the Anaconda distribution and I've tried installing pygraphviz using several different channels but it always gave me the same error. I ended up manually downloading the .whl file and installing it using pip as shown in:

howto install pygraphviz on windows 10 64bit

I'm running a windows 10 64 bit machine with python version 3.4. Here's an example of some code I tried to run (copied from the examples folder in the pygraphviz installation):

from __future__ import absolute_import
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division

import pygraphviz as pgv

A=pgv.AGraph(directed=True,strict=True,rankdir='LR')

A.add_node(1,color='red') 
A.add_node(5,color='blue')

A.add_edge(1,2,color='green')
A.add_edge(2,3)
A.add_edge(1,3)
A.add_edge(3,4)
A.add_edge(3,5)
A.add_edge(3,6)
A.add_edge(4,6)

A.graph_attr['epsilon']='0.001'
print(A.string()) # print dot file to standard output
A.layout('dot') # layout with dot
A.draw('foo.ps') # write to file

This gives me the error "AttributeError: module 'pygraphviz' has no attribute 'AGraph'". Any help would be appreciated!

1 Answers1

0

In my case, I had to replace following lines in my older program (pygraphviz working with python 2.7) :

my_graph = graphviz.AGraph(id="my_graph", name="my_graph", file=self.fileName)

my_graph.layout(prog='dot')

my_graph.draw(path="my_graph3.svg", format="svg")

with:

from graphviz import Source, render

my_graph = graphviz.Digraph(name="my_graph", engine='dot')
my_graph.src = Source(srcStr, filename=None, directory=None, format='svg', engine='dot', encoding='utf-8')
my_graph.src.render('dot', 'svg', 'my_graph3.svg',renderer='cairo', formatter='cairo')

The srcStr is a string like:

digraph "pet-shop" {
    graph [rankdir=LR]
    node [shape=plaintext]
    edge [arrowhead=vee arrowsize=2]
    parrot
    dead
    parrot -> dead
}

Although i thought this would create a my_graph3.svg file, it actually creates a folder svg with the file 'dot.cairo.cairo.svg' inside it.

Also, it took a lot of trial and error to get this, so I am posting it if it helps. Any comments related to improving the parameters are appreciated.

Deepak Garud
  • 977
  • 10
  • 18