简体   繁体   中英

How to run iPython **shell** magic from a script?

I've found a way to run magic commands, eg

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

but how do you run something like !ls ?

Specifically, I need this to run a console script at runtime in Google Colab.

I have a library called dimcli that needs to create a local credentials file. Normally I'd invoke it like this !dimcli --init . However I'd like to run that command automatically each time the library gets loaded.

Think I got it

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM