问题
The code is written by someone else using Python 2.7.12, networkx 1.11, numpy 1.13.0, scipy 0.18.1, matplotlib 2.0.2. It consists of several self-made modules. I have converted the entire code to Python 3.x using "2to3" converter. I am running the entire code using Spyder which has all the latest packages.
Now while running the code I am getting AttributeError: 'DiGraph' object has no attribute '_node'
error. Following is the error message in spyder.
runfile('C:/Users/NaVnEeT/Desktop/Thesis/WIP2/main.py', wdir='C:/Users/NaVnEeT/Desktop/Thesis/WIP2')
__main__ : INFO main() started
Settings : INFO Settings __init__ finished
Settings : INFO Settings __init__ finished
Settings : INFO Settings __init__ finished
Settings : INFO Settings __init__ finished
Settings : INFO Settings __init__ finished
Settings : INFO Settings __init__ finished
Settings : INFO Settings __init__ finished
Settings : INFO Settings __init__ finished
Settings : INFO Settings __init__ finished
Settings : INFO Settings __init__ finished
TransGrph : INFO Graph is loaded from file
Traceback (most recent call last):
File "<ipython-input-1-c61da2f96623>", line 1, in <module>
runfile('C:/Users/NaVnEeT/Desktop/Thesis/WIP2/main.py', wdir='C:/Users/NaVnEeT/Desktop/Thesis/WIP2')
File "C:\Users\NaVnEeT\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 710, in runfile
execfile(filename, namespace)
File "C:\Users\NaVnEeT\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/NaVnEeT/Desktop/Thesis/WIP2/main.py", line 137, in <module>
main()
File "C:/Users/NaVnEeT/Desktop/Thesis/WIP2/main.py", line 92, in main
ch8NLR()
File "C:\Users\NaVnEeT\Desktop\Thesis\WIP2\code\thesis\ch8NLR.py", line 176, in ch8NLR
graph = TransportGraph(settings[0])
File "C:\Users\NaVnEeT\Desktop\Thesis\WIP2\code\buildgraph\transportgraph.py", line 58, in __init__
self.dmatrix = add_dmatrix(self.G, settings)
File "C:\Users\NaVnEeT\Desktop\Thesis\WIP2\code\buildgraph\demandmatrix.py", line 20, in add_dmatrix
origdest_graph = [x for x in G.nodes() if x[0] == 'F']
File "C:\Users\NaVnEeT\Anaconda3\lib\site-packages\networkx\classes\graph.py", line 717, in nodes
nodes = NodeView(self)
File "C:\Users\NaVnEeT\Anaconda3\lib\site-packages\networkx\classes\reportviews.py", line 168, in __init__
self._nodes = graph._node
AttributeError: 'DiGraph' object has no attribute '_node'
I can provide the part of the code if required for a solution.
Note: the code is running successfully with the old packages.
回答1:
If it runs in the old version, but not now, this error is probably caused by the old code being incompatible with networkx 2.x. You should read the migration guide for updating 1.x code to run in networkx 2.0.
edit: from comments, it's now clear that the problem was that the graph was created in 1.x and then pickled. So it has the 1.x attributes. But now it's being used with 2.x code which expects 2.x attributes. The solution is explained here.
来源:https://stackoverflow.com/questions/48647975/attributeerror-digraph-object-has-no-attribute-node