Using IPython as shell

IPython creates and interactive environment within your shell application. IPython is widely used and few of its features are listed below:

  • Tab completion
  • History mechanism
  • Inline editing
  • Calls external Python scripts using the command %run
  • Access to system commands
  • Access to Python debugger and profiler

Below are few of the things you should now to get started using the IPython.

Start a new IPython session

To start a new session with IPython you type the following command:

(data_analysis) ddn_ro@Linux:~/env/data_analysis$ ipython3
Python 3.5.2 (default, Aug 18 2017, 17:48:00) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: 

You can quit the session by typing quit() in the command line or by pressing Ctrl+D

Saving an IPython session

You may want to save a session in order to be able to access it later. You can do that by running the command %logstart in the IPython shell.

In [1]: %logstart
Activating auto-logging. Current session state plus future input saved.
Filename       :
Mode           : rotate
Output logging : False
Raw input log  : False
Timestamping   : False
State          : active

In [2]: 

Logging can be switched off by running the command %logoff in the IPython shell.

Execute a system shell command in IPython session

As I said, IPython can access system shell commands. It is done by prefixing the command with an exclamation mark (!).

In [2]: !date
Thu Sep 21 05:17:33 ICT 2017

In [3]: 

The command output can be stored as shown below:

In [3]: todaydate = !date

In [4]: todaydate
Out[4]: ['Thu Sep 21 05:19:16 ICT 2017']

In [5]: 

Display history commands in IPython session

You can print in the shell a list of the previous commands using %hist as shown below:

In [5]: %hist %logstart !date todaydate = !date todaydate %hist In [6]:

You can also search through history using the following command:

In [6]: %hist -g date
 1/3: !date
 1/4: thedate = !date
 1/5: thedate
 1/8: %hist -g !date
   2: !date
   3: todaydate = !date
   4: todaydate
   6: %hist -g date

In [7]: 

Leave a Reply