python - ImportError:无法导入名称'treewidth'
问题描述
我一直在尝试从 NetworkX 库中导入必要的模块,但它不断抛出 ImportError 或 AttributeError。
我在最后一年的项目中使用 NetworkX 库,我想使用这个函数来计算给定图的树宽分解,我需要其他函数的值。
我尝试了以下事情:
- 最初我尝试导入文档中建议的方式
import networkx as nx
from networkx.algorithms import approximation
G = nx.barbell_graph(5, 1)
decom = approximation.treewidth_min_fill_in(G)
但这只会给我以下错误:
AttributeError: module 'networkx.algorithms.approximation' has no attribute 'treewidth_min_fill_in'
- 我还尝试使用以下方法直接导入它:
import networkx as nx
G = nx.barbell_graph(5, 1)
decom = nx.algorithms.approximation.treewidth.treewidth_min_fill_in(G)
但这只会给我以下错误:
AttributeError: module 'networkx.algorithms.approximation' has no attribute 'treewidth'
- 然后我在这里找到了一个可能的解决方案,它仅在派系的情况下有效,但在尝试导入“treewidth”时无效。
import networkx as nx
from networkx.algorithms.approximation import treewidth
G = nx.barbell_graph(5, 1)
decom = treewidth.treewidth_min_fill_in(G)
但这只会给我以下错误:
ImportError: cannot import name 'treewidth'
我没有成功找到解决方案。任何帮助表示赞赏!
注意:这是我关于 SO 的第一个问题,因此请就这个问题的格式提供任何额外的反馈。
解决方案
你使用哪个版本的 networkx 包,你是如何安装它的?
我尝试pip install networkx
使用 Python 3.7 执行然后执行您的代码:
import networkx as nx
from networkx.algorithms import approximation
G = nx.barbell_graph(5, 1)
decom = approximation.treewidth_min_fill_in(G)
print(decom)
输出是:
(4, <networkx.classes.graph.Graph object at 0x1070d4ef0>)
您可以尝试使用以下方法查看模块的版本:
import networkx
print(networkx.__version__)
以及使用了哪些方法:
import networkx
from networkx.algorithms import approximation
print(dir(approximation))
推荐阅读
- winapi - 控制面板小程序执行命令行语法
- android - 从 Flow 接收对象后,如何在挂起函数中返回对象?
- c# - 是否可以从 DataGrid 访问 DataGridRow?
- .net - 在 Visual Studio 2019 中,如何检测构建配置更改?
- autodesk-forge - 如何为 Autodesk Forge App 指定多个回调 URL?
- html - 在网站上实现 Chrome DevTool 设备模式
- c# - SpecRun 在通过 CLI 运行测试时不运行任何测试
- xml - 解析 xml 并生成文档而不会丢失换行符
- lua - 为什么需要将栈底创建的大字符串保存在 Lua 字符串缓冲区中
- excel - 单击按钮后添加行