【问题标题】:How to run iPython **shell** magic from a script?如何从脚本运行 iPython **shell** 魔法?
【发布时间】:2019-10-15 10:31:29
【问题描述】:

我找到了一种运行魔法命令的方法,例如

from IPython import get_ipython
get_ipython().run_line_magic('matplotlib', 'inline')

但是你如何运行类似 !ls 的东西?

具体来说,我需要它在运行时在 Google Colab 中运行控制台脚本。

我有一个名为dimcli 的库,它需要创建一个本地凭据文件。通常我会像这样!dimcli --init 调用它。但是,我想在每次加载库时自动运行该命令。

【问题讨论】:

    标签: python jupyter-notebook google-colaboratory ipython-magic


    【解决方案1】:

    我想我明白了

    from IPython import get_ipython
    get_ipython().run_line_magic("sx", "ls")
    

    https://github.com/ipython/ipython/blob/master/IPython/core/magics/osm.py#L668

    【讨论】:

      猜你喜欢
      • 2012-05-08
      • 2014-02-27
      • 2016-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-20
      相关资源
      最近更新 更多