ipython how to execute several history lines

我是研究僧i 提交于 2019-12-20 08:37:00

问题


In ipython, we can use

_ih[32:39] 

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


回答1:


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



回答2:


I use the list notation:

exec In[34:36]

also, if you use the edit function to edit a chunk, the Out list will have your code in it, so:

exec Out[35]

And my favorite:

edit In[34:38]

because I am a fat-fingered slob who can rarely get it right on the first try.




回答3:


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




回答4:


try %recall, check doc of recall magic command




回答5:


Use the exec statement:

exec(_ih[32:39])

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




回答6:


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



回答7:


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.



来源:https://stackoverflow.com/questions/7613044/ipython-how-to-execute-several-history-lines

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!