Getting started with ipython
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.
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
After installation, run it using your default Python (2 or 3) using:
Or to use Python 3:
This gives you an introduction and overview of IPython’s features.
This lists all methods and fields of the object and its documentation (if it exists).
Same as above, provides even more detail about the object, in particular will try to find and display the source code for the object.
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.
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.
jupyter qtconsole, or
ipython qtconsole before version 5, will start a multi-tabbed QT based 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.
Pasting into IPython
This is the primary Magic Method for pasting. It directly pastes text from the system clipboard, intelligently handling common issues with newlines and indentation.
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
Store variables on IPython
%storemagic stores variables and macros on IPython’s database.
To automatically restore stored variables at startup add this to
c.StoreMagic.autorestore = True
In : l = ['hello',10,'world'] In : %store l In : exit (IPython session is closed and started again...) [email protected]:~$ ipython In : l Out: ['hello', 10, 'world']
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.