How to Run an Ipython Magic from a Script (Or Timing a Python Script)

How to run an IPython magic from a script (or timing a Python script)

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.

Time python scripts using IPython magic

Solution

Your can use:

%%timeit
%run script.py input_param1 input_param2

beware that the script will be executed multiple times (the number is adaptive). To execute it only once (and have less accurate timing) change the first line to

%%timeit -n1 -r1

Explanation

All the magic commands starting with %% apply to the whole cell. In particular %%timeit will time all the lines in the cell.

IPython allows to use magic commands (single %) in any point of your code (i.e. loops, if-then). Here we just use the magic command %run to run the script.

See also: Magic functions from the official IPython docs.

Could we run ipython commands in python?

Any method for running your IPython code using a standard Python interpreter is going to be a little complicated. For example, see this question, with one of the answers illustrating the call to IPython's 'magic' methods to do a shell command:

from IPython.terminal.embed import InteractiveShellEmbed

ipshell = InteractiveShellEmbed()
ipshell.dummy_mode = True
ipshell.magic("%timeit abs(-42)")

The much easier option would be to simply use the IPython interpreter to run your saved script. You need to make sure that each shell command is preceded by a %, as this indicates a 'magic' command. Should be a simple find-and-replace task, as I doubt you are using too many shell commands. If there are a lot of different shell commands to prefix with % you could also write a short script to do this work for you. You also need to ensure that your script has the extension .ipy.

script.ipy:

%cd ..
%ls
x = "My script!"
print(x)

To run script from terminal:

>>> ipython script.ipy

Import functions that use ipython magic

Use case solution

If you want to reuse code that has ipython magic functions in it, simply use %run instead of the import.

ipython:

%run utils.ipynb

utils.ipynb:

def asdf():
print("asdf")
!echo asdf

Example above works fine.

Taken from this answer.


On reusing ipython code

Why the original question was an A-B problem of some kind? The reason is that you should not really drag ipython additional functionality to the pure python execution.

  • If you have a, let's say, google colab notebook with useful utils,
    then just !wget it from the public link and %run it.
  • If you have a python script to execute, do a regular import on .py files.

Sure, there are ways to import .ipynb files to python code: Jupyter notebook's Notebook Loader, ipynb package, nbimporter package (but even the nbimporter's author says you should not really do that. Just convert your notebook to .py using VSCode or another tool).

So, if you use ipython research environment and features, just stick with it. Or switch to pure python environment with proper modules, testing, e.t.c.


Also, if you're struggling with doing wget:

file_id = "1xE8Db1zvQ7v-z13CO2qc3F6wJmtr3YHu" # can be extracted from public link
file_out_name = "utils.ipynb"
!wget "https://docs.google.com/uc?export=download&id=""$file_id" -O "$file_out_name"

However, on the problem of calling cmd, I see no way of executing shell commands interactively in python with one line. While subprocess can do that, it takes multiple lines to achieve.

How to use IPython magic within a script to auto reload modules?

Thank you for the link Gall!!! Whit your help I came up with the following solution:

from IPython import get_ipython
ipython = get_ipython()

if '__IPYTHON__' in globals():
ipython.magic('load_ext autoreload')
ipython.magic('autoreload 2')


Related Topics



Leave a reply



Submit