【问题标题】:Interesting import behavior in ipythonipython 中有趣的导入行为
【发布时间】:2016-03-27 04:25:50
【问题描述】:

我在玩 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 example.py`从ipython运行时没有错误

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

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

【问题讨论】:

    标签: python ipython python-import graph-tool


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      import graph_tool.all as gt
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-15
        • 1970-01-01
        • 2017-02-07
        • 2010-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多