如何从脚本运行 iPython **line** 魔法? [重复]

问题描述 投票:0回答:1

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

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个回答
1
投票

我想我明白了

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

参见https://github.com/ipython/ipython/blob/8.16.x/IPython/core/magics/osm.py#L681

© www.soinside.com 2019 - 2024. All rights reserved.