It depends a bit on which version of IPython you have. If you have 1.x:
from IPython import get_ipython
ipython = get_ipython()
If you have an older version:
import IPython.core.ipapi
ipython = IPython.core.ipapi.get()
or
import IPython.ipapi
ipython = IPython.ipapi.get()
Once that’s done, run a magic command like this:
ipython.magic("timeit abs(-42)")
Note that the script must be run via ipython
.