ipython how to execute several history lines

前端 未结 7 1522
梦谈多话
梦谈多话 2021-01-30 14:04

In ipython, we can use

_ih[32:39] 

To show history lines between 32 and 39. How can I directly execute these history lines?

相关标签:
7条回答
  • 2021-01-30 14:32

    Use the exec statement:

    exec(_ih[32:39])
    

    http://docs.python.org/reference/simple_stmts.html#exec

    0 讨论(0)
  • 2021-01-30 14:40

    You can execute code from previous sessions with %recall. See %recall documentation here.

    #Execute all code from previous session.
    %recall ~1/
    
    #Execute all code from two sessions previous the current session.
    %recall ~2/
    
    #Execute lines 1 to 5 from previous session.
    %recall ~1/1-5
    
    0 讨论(0)
  • 2021-01-30 14:51

    try %recall, check doc of recall magic command

    0 讨论(0)
  • 2021-01-30 14:53

    You can edit lines before executing them like so:

    edit 1-5
    

    Apparently the syntax changed at some point from the list-notation used in dreynold's answer.

    0 讨论(0)
  • 2021-01-30 14:56

    On recent versions of iPython you use the rerun magic-comand:

    %rerun 32:39

    Documentation on that command: http://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-rerun

    0 讨论(0)
  • 2021-01-30 14:56

    You can create a named macro from the lines and execute them:

    %macro foo 32-38
    foo
    

    This is useful if you want to execute the same set of lines more than once. Also the lines do not need to be sequential or in order:

    %macro bar 38 37 32-36 42
    
    0 讨论(0)
提交回复
热议问题