Search code examples
python-3.xnlpvirtualenvnetworkxattributeerror

AttributeError: module 'networkx' has no attribute 'nx_pydot'


I'm trying to execute a python file named demo.py (which imports another file takahe2.py) in python virtual environment with python3.7. I am trying to get the output of some strings as text in a .txt file. But when I run the code it shows the output in the console but failed to create a .txt file, showing this error:

File "/home/user/takahe2.py", line 932, in write_dot

nx.nx_pydot.write_dot(self.graph, dotfile)

AttributeError: module 'networkx' has no attribute 'nx_pydot'

In takahe2.py:

I have imported these:

from networkx.drawing.nx_agraph import write_dot from networkx.drawing.nx_pydot import write_dot

Corresponding function in takahe2.py:

 `def write_dot(self, dotfile):`
    """ Outputs the word graph in dot format in the specified file. """
    
    # nx.write_dot(self.graph, dotfile)
    nx.nx_pydot.write_dot(self.graph, dotfile)`

The actual file I'm trying to run demo.py: (Portion of the code is shown where error occurred:)

for i in range(1,140):

serial_no = str(i)
document = open('DataSet/Source/'+serial_no+'.txt').read()
doc = sentTokenizer.sentTokenizing().sentTokenize(document)
print('doc',doc)

filenamee, n = clustering.startF(doc)
print("\n\nSource:",document)

summary = getSummary(filenamee)
print('\n\nSummary:',summary)

# save the summary
createFolder('DataSet/')
fi = open('DataSet/'+serial_no+'.txt','+w')
fi.write(summary)

My networkx version is 1.7. Os: Linux


Solution

    1. Use nx.drawing.nx_pydot.write_dot(self.graph, dotfile) instead of nx.nx_pydot.write_dot(self.graph, dotfile) in the takahe2.py file.

    2. Install this package:

      pip install pydot

    3. Don't forget to import these in your file:

      import networkx as nx

      from networkx.drawing.nx_agraph import write_dot

      from networkx.drawing.nx_pydot import write_dot

      import matplotlib.pyplot as plt

    For safety, I would install the following packages as well.

     pip install pydotplus
     pip install pyqt5
     sudo apt install libxcb-xinerama0 
     sudo apt-get install graphviz graphviz-dev
     pip install pygraphviz