Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ipython how to execute several history lines

Tags:

python

ipython

In ipython, we can use

_ih[32:39] 

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

like image 997
wiswit Avatar asked Sep 30 '11 16:09

wiswit


People also ask

How do I invoke IPython?

You start IPython by typing “ipython” in your terminal. $ ipython Python 2.7. 2 (default, Jun 20 2012, 16:23:33) Type "copyright", "credits" or "license" for more information. IPython 0.13.

How do I view IPython history?

Use '%hist -g' to show full saved history (may be very long). get the last n lines from all sessions. Specify n as a single arg, or the default is the last 10 lines.

How do I start IPython from command prompt?

The easiest way is to run easy_install ipython[all] as an administrator (start button, type cmd , shift+right click on “cmd.exe” and select “Run as administrator”). This installs the latest stable version of IPython including the main required and optional dependencies.

What is IPython command?

IPython bridges this gap, and gives you a syntax for executing shell commands directly from within the IPython terminal. The magic happens with the exclamation point: anything appearing after ! on a line will be executed not by the Python kernel, but by the system command-line.


3 Answers

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 
like image 60
ssoler Avatar answered Sep 20 '22 16:09

ssoler


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.

like image 26
dreynold Avatar answered Sep 20 '22 16:09

dreynold


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

like image 20
Christian Herenz Avatar answered Sep 16 '22 16:09

Christian Herenz