我找到了一种运行魔法命令的方法,例如
from IPython import get_ipython
get_ipython().run_line_magic('matplotlib', 'inline')
但是你如何运行像
!ls
这样的东西呢?
具体来说,我需要它在 Google Colab 中运行时运行控制台脚本。
我有一个名为
dimcli
的库,需要创建本地凭据文件。通常我会像这样调用它!dimcli --init
。不过,我想在每次加载库时自动运行该命令。
我想我明白了
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