Getting started with ipython

Download ipython eBook

Remarks

IPython is a Read-Evaluate-Print Loop shell for interactive Python development. It supports interactive visualizations using GUI toolkits, and provides a kernel for Jupyter. It can also be embedded into other projects.

There are other similar REPL shells for Python, for example, ptpython and bpython.

Versions

VersionRelease Date
5.0.02016-07-07
4.2.02016-04-20
4.1.02016-02-02
4.0.02015-08-12
3.2.02015-06-21
3.1.02015-04-03
3.0.02015-02-27
2.4.02015-01-30
2.3.02014-10-01
2.2.02014-08-06
2.1.02014-05-21
2.0.02014-04-02
0.132012-06-30
0.122011-12-19
0.112011-07-31
0.102010-09-01
0.92008-09-13

Installation and Usage

Like the built-in python interactive shell, IPython is a REPL (Read-Evaluate-Print Loop) shell, with a variety of features that make it more pleasant to use for day-to-day Python development than the built-in REPL shell.

Installation

To install it:

pip install ipython
 

Or, via Anaconda:

# To install into the active environment:
$ conda install ipython 

# Or, to create a new environment with IPython installed:
$ conda create -n <env_name> ipython
 

Or, via Enthought Canopy:

$ enpkg ipython
 

Usage

After installation, run it using your default Python (2 or 3) using:

ipython
 

Or to use Python 3:

ipython3
 

ipython shell

Getting Help

?
 

This gives you an introduction and overview of IPython's features.

object? 
 

This lists all methods and fields of the object and its documentation (if it exists).

object??
 

Same as above, provides even more detail about the object, in particular will try to find and display the source code for the object.

object.<TAB Key>
 

TAB-completion that lists and iterates through available fields/methods of an object. Due to the dynamic nature of Python not all methods can be discovered this way. Also private methods (starting with _ ) will be hidden by default, insert a _ and press TAB again to display them.

%quickref
 

This displays a quick-reference for the IPython shell.

IPython vs Jupyter

IPython has two parts to it: A command line interface that replaces the default python REPL and a way to run Python through the web browser as a graphical user interface.

With the latest developments the browser part has been split into the Jupyter project that enables multiple programming languages to use the graphical interface. It is still possible to use IPython as a Python kernel for this.

Up to date setup instructions for Jupyter can be found in the official install docs.

ipython , or jupyter console , when invoked from the command line without any other parameters will enter an interactive terminal session as below: Jupyter Console

jupyter qtconsole , or ipython qtconsole before version 5, will start a multi-tabbed QT based console:

QT Console

jupyter notebook , or ipython notebook before version 5, will start a server and by default open up a web page, at http://localhost:8888/tree , with the "Home" view of the current directory. This allows you to open existing notebooks or new kernels in several languages, depending on which you have installed; each will be opened in a new browser tab.

Notebooks allow you to mix markdown, including MathJax, code from the kernel of your choice, plots and graphs, images and even videos. Home Viewenter image description here

Pasting into IPython

%paste
 

%paste demo

This is the primary Magic Method for pasting. It directly pastes text from the system clipboard, intelligently handling common issues with newlines and indentation.

%cpaste
 

%cpaste demo

If you are using IPython via SSH, use %cpaste instead, as it does not need to access the remote system clipboard.

Since IPython 5.0.0, the improved prompt toolkit should directly handle pasting multi-line code without the need for %paste or %cpaste .

Store variables on IPython

%storemagic stores variables and macros on IPython's database. To automatically restore stored variables at startup add this to ipython_config.py :

c.StoreMagic.autorestore = True
 

Example:

In [1]: l = ['hello',10,'world']
In [2]: %store l
In [3]: exit

(IPython session is closed and started again...)

ville@badger:~$ ipython
In [1]: l
Out[1]: ['hello', 10, 'world']
 

Note:

It should be noted that if you change the value of a variable, you need to %store it again if you want to persist the new value.

Note also that the variables will need to be pickleable; most basic python types can be safely %store’d.

Stats

94 Contributors: 13
Sunday, August 14, 2016
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook