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'

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

注意:代码使用旧包成功运行。

4

1 回答 1

3

如果在旧版本上运行,但现在没有,这个错误可能是旧代码与networkx 2.x不兼容造成的。您应该阅读迁移指南以更新 1.x 代码以在 networkx 2.0 中运行。

编辑:从评论中,现在很明显问题在于该图是在 1.x 中创建的,然后经过腌制。所以它具有 1.x 属性。但现在它与需要 2.x 属性的 2.x 代码一起使用。解决方案在这里解释。

于 2018-02-06T22:21:00.540 回答