Search code examples
pythonipythonmagic-function

ipython use "%run" to execute a subset of a file


Is there some way I can use %run to execute a subset of a file?

Something like this:

$ ipython
In [1]: %run my_code.py -L 20 100 # executes lines 20-100 in ipython interpereter

Solution

  • %run doesn't have an option for this. You can see all the options it takes by doing %run? inside IPython.

    However, you can bring a specific range of lines from a file into the interactive prompt, and run it from there. The syntax to do this looks like:

    %load -r 20-100 my_code.py