属性错误:'DiGraph'对象没有'_node'属性。

3
这段代码是由其他人使用Python 2.7.12、networkx 1.11、numpy 1.13.0、scipy 0.18.1和matplotlib 2.0.2编写的,由几个自制模块组成。我使用“2to3”转换器将整个代码转换为Python 3.x。我正在使用具有所有最新软件包的Spyder运行整个代码。
现在,在运行代码时,我遇到了“AttributeError:'DiGraph' object has no attribute '_node'”错误。以下是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'

如果需要解决方案,我可以提供代码的部分。

注意:旧包能够成功运行该代码。

1个回答

4
如果旧版本可以运行,但现在不能运行,则此错误可能是由于旧代码与networkx 2.x不兼容。您应该阅读迁移指南,以更新1.x代码以在networkx 2.0中运行。
编辑:根据评论,现在清楚的是问题是图形是在1.x中创建的,然后进行了pickled。因此它具有1.x属性。但现在它正在与期望2.x属性的2.x代码一起使用。解决方案在这里说明。

谢谢您的回复。您是正确的,这是networkx版本的问题。我已经通过降级networkx库但保持其余代码在3.x版本中进行了重新确认。这个networkx代码部分分布在很多函数和库中,我将尝试将它放在一个单独的文件中,然后在这里发布。如果可能的话,您能否给我大致介绍一下这个错误“AttributeError: 'DiGraph' object has no attribute '_node'”? - kumar navneet
你能告诉我如何定义图形吗(它是从现有的pickled对象中加载的吗)?错误发生在networkx代码的一行中,该行引用了graph._node。但是DiGraphs(至少在networkx 2.x中)具有该属性。所以我唯一能想到的就是DiGraph是在1.x中创建的,因此它只具有1.x属性,但是它被视为2.x DiGraph。 - Joel
抱歉延迟回复。是的,您是正确的,它是使用1.x创建的,代码使用现有的Pickle文件进行进一步处理。我使用2.x创建了图表,现在它运行良好。 - kumar navneet

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接