ipython 中有趣的导入行为

Interesting import behavior in ipython

我在玩 graph_tool 时注意到了这一点。某些模块属性似乎仅在 运行 来自 ipython 时才可用。最简单的例子(example.py)

import graph_tool as gt

g = gt.Graph()
gt.draw.sfdp_layout(g)

使用 run example.y', but from the command line,python 从 ipython 无错误地运行 example.py` 产生

AttributeError: 'module' object has no attribute 'draw'

ipython example.py也是如此。我不知道是什么原因造成的。我想访问绘图模块,但似乎我只能通过 from graph_tool.draw import * 执行此操作,如有任何帮助或解释,我们将不胜感激。

导入图形工具时,将其导入为:

import graph_tool.all as gt

这将从 graph-tool 导入所有模块,如果安装了所有必要的东西,这应该适合你。

您应该显式导入所有正在使用的模块。在您的情况下,您需要添加例如import graph_tool.draw as gt_draw(仅添加 import graph_tool.draw 可能就足够了,但这段代码可能被认为不明显)。